0

我很新的MVC,并仍在努力学习。我一直在研究几个小时,并没有找到我在找什么。我有两个链接表,table1和table2。比方说,table2包含/显示来自table1的ID,而不是我希望显示链接到ID的值,例如:名称,而不是当我的视图处于创建或详细信息时的值,但是在编辑或创建时,我希望显示一个用户选择名称的下拉菜单。就像我说过的,我已经研究了很长时间,一直无法找到答案。我没有代码可以显示,但向正确的方向提示将非常有帮助。MVC下拉菜单使用链接表

+0

假设你已经正确地配置你的实体框架模型,看看在[MVC dropdownlistfor]( https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet- MVC)。 –

回答

0

首先,创建表:

--instead of Breaz, use your database name 
USE [Breaz] 
GO 
/****** Object: Table [dbo].[table1] Script Date: 5/1/2017 10:11:40 AM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[table1](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [name] [varchar](20) NULL, 
CONSTRAINT [PK_table1] PRIMARY KEY CLUSTERED 
(
    [Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[table2] Script Date: 5/1/2017 10:11:40 AM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[table2](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [table1Id] [int] NULL, 
    [table2Desc] [varchar](20) NULL, 
CONSTRAINT [PK_table2] PRIMARY KEY CLUSTERED 
(
    [Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
SET IDENTITY_INSERT [dbo].[table1] ON 

GO 
INSERT [dbo].[table1] ([Id], [name]) VALUES (1, N'ddl1') 
GO 
INSERT [dbo].[table1] ([Id], [name]) VALUES (2, N'ddl2') 
GO 
INSERT [dbo].[table1] ([Id], [name]) VALUES (3, N'ddl3') 
GO 
SET IDENTITY_INSERT [dbo].[table1] OFF 
GO 
SET IDENTITY_INSERT [dbo].[table2] ON 

GO 
INSERT [dbo].[table2] ([Id], [table1Id], [table2Desc]) VALUES (1, 1, N'select1') 
GO 
INSERT [dbo].[table2] ([Id], [table1Id], [table2Desc]) VALUES (2, 2, N'select2') 
GO 
SET IDENTITY_INSERT [dbo].[table2] OFF 
GO 
ALTER TABLE [dbo].[table2] WITH CHECK ADD CONSTRAINT [FK_table2_table1] FOREIGN KEY([table1Id]) 
REFERENCES [dbo].[table1] ([Id]) 
GO 
ALTER TABLE [dbo].[table2] CHECK CONSTRAINT [FK_table2_table1] 
GO 

创建EDMX文件中像这样:

右键单击Models文件夹,然后单击添加 - > ADO.DATA实体模型,将其命名为aDropDown。点击确定。从数据库生成突出显示已经单击下一步。单击新建连接...将您的服务器名称放在服务器名称框中。我使用。\ sqlexpress。如果您使用用户名和密码,请单击使用Windows身份验证,然后输入您的凭据并单击保存我的密码。从下拉列表中选择您的数据库名称。点击确定。如果您输入了密码,请等到您可以回答Y,然后加入敏感信息。在“保存实体连接设置...”下将名称复制到剪贴板中。单击下一步。展开表格,然后检查表格1和表格2,然后单击完成。点击确定两次到出现的窗口。关闭图表。

这是你的控制器/视图:

public class DDLModel 
{ 
    public DDLModel() 
    { 
     List<SelectListItem> list = new List<SelectListItem>(); 

     try 
     { 
      //BreazEntities16 for you should be the item that you had copied to the clipboard 
      //before, or check the aDropDown.Context.cs file 
      using (BreazEntities16 entity = new BreazEntities16()) 
      { 
       entity.table1. 
        OrderBy(r => r.name).ToList().ForEach(r => list.Add(
        new SelectListItem { Text = r.name, Value = r.Id.ToString() })); 
      } 
     } 
     catch (Exception e) 
     { } 

     //add <select> to first item 
     list.Insert(0, new SelectListItem { Text = "", Value = "" }); 
     Table1List = list; 
    } 

    public List<SelectListItem> Table1List { get; set; } 

    //If you want to put [Required] or other attributes into your model property, and 
    //you think you don't want to directly use table2, please use DO use the direct table2, 
    //Just see the following post 
    //http://stackoverflow.com/questions/14059455/adding-validation-attributes-with-an-entity-framework-data-model 
    public table2 table2 { get; set; } 
} 

public class HomeController : Controller 
{ 
    //I use Index60, you should use Index or what the RouteConfig points to 
    [HttpPost] 
    public ActionResult Index60(DDLModel ddlModel) 
    { 
     //BreazEntities16 for you should be the item that you had copied to the clipboard 
     //before, or check the aDropDown.Context.cs file 
     using (BreazEntities16 entity = new BreazEntities16()) 
     { 
      //modify the tableId of table2 
      entity.table2.Attach(ddlModel.table2); 
      entity.Entry(ddlModel.table2).Property(x => x.table1Id).IsModified = true; 
      entity.Configuration.ValidateOnSaveEnabled = false; 
      entity.SaveChanges(); //tableid changes, not table2Desc 
     } 

     return View(ddlModel); 
    } 

    //I use Index60, you should use Index or what the RouteConfig points to 
    public ActionResult Index60() 
    { 
     DDLModel ddlModel = new DDLModel(); 

     //BreazEntities16 for you should be the item that you had copied to the clipboard 
     //before, or check the aDropDown.Context.cs file 
     using (BreazEntities16 entity = new BreazEntities16()) 
     { 
      //make sure there is using System.Data.Entity at top 
      //I am getting first but you can .Where to find another 
      ddlModel.table2 = entity.table2.Include(q => q.table1).Where(r => r.Id == 1).FirstOrDefault(); 
     } 

     return View(ddlModel); 
    } 

这是你的看法:

@model Testy20161006.Controllers.DDLModel 
@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index60</title> 
</head> 
<body> 
    @using (Html.BeginForm()) 
    { 
     <div> 
      <table> 
       <tr> 
        <td> 
         @*need the hidden id*@ 
         @Html.HiddenFor(r=>r.table2.Id) 

         @Html.LabelFor(r => r.table2.table1Id) 
        </td> 
        <td> 
         @Html.DropDownListFor(m => m.table2.table1Id, 
        new SelectList(Model.Table1List, "Value", "Text")) 
         @Html.ValidationMessageFor(model => model.table2.table1Id) 
        </td> 
       </tr> 
      </table> 
     </div> 
     <input type="submit" value="submit" /> 
    } 
</body> 
</html>