【发布时间】:2014-10-23 13:06:35
【问题描述】:
在 ASP.NET-MVC 5 应用程序中,我有以下模型
class Employee {
int EmployeeID {get;set;}
string FirstName {get;set;}
List<OfficeLocations> OfficeLocations {get;set;}
}
class OfficeLocations {
int OfficeLocationsID {get;set;}
//foreign key
int EmployeeID {get;set;}
string Value1 {get;set;}
string Value2 {get;set;}
}
我有一个编辑视图,用于修改或添加员工可能所属的不同办公地点。它看起来像这样:
@model List<Project.Models.OfficeLocations>
@for (int i = 0; i < Model.Count; i++) {
@Html.EditorFor(m => m[i].CitLocation, new { htmlAttributes = new { @class = "my_editor" } })
@Html.HiddenFor(m => m[i].OfficeLocationsID)
@Html.HiddenFor(m => m[i].EmployeeID)
}
//extra editor box for adding a new value
@Html.Editorfor(??????.Value)
对于如何在数据库表中的模型(列表)中添加新条目,我有点困惑。我在额外的 Editorfor 框的参数中输入了什么(所有 ???? 都在哪里)
另外,控制器操作方法应该是什么样子的?
【问题讨论】:
-
为此,您将需要 javascript/jquery。 This example 可能会帮助您入门
-
谢谢你的例子,你介意添加一个问题的答案,并添加更多细节,比如 post 方法接受什么作为参数?与最初传递给视图的列表相同吗?
-
我可以添加一个更详细的答案,但有几件事我不明白。您是否希望您的
@model成为Employee,以便您可以将OfficeLocations添加到员工,或者只是@model List<OfficeLocations>在这种情况下您如何传递EmployeeID?@Html.EditorFor(m => m[i].Value1是什么 - 你没有名为Value1的属性 - 你的意思是m => m[i].CityLocation吗? -
大声笑好点..实际上我会更新我的代码,但是我需要模型是 List
并且我可以通过执行 m => m[i].EmployeeID 来传递模型 -
使用反引号。见Comment formatting(点击评论栏右侧的帮助,然后了解更多)
标签: c# asp.net asp.net-mvc entity-framework