[英]Populate form from table select with asp.net mvc
我有一个包含数据的表,单击编辑按钮时,如何用数据填充同一页面上的表单。 基本上是应该与此示例相同,但不使用淘汰表
http://jsfiddle.net/jiggle/2cr2f/
@model IEnumerable<GenomindApp2.Areas.RulesEngine.ViewModels.GeneViewModel>
@{
ViewBag.Title = "Index2";
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.GeneValue)
</th>
<th>
@Html.DisplayNameFor(model => model.GeneCode)
</th>
<th>
@Html.DisplayNameFor(model => model.GeneName)
</th>
<th>
@Html.DisplayNameFor(model => model.GeneComments)
</th>
<th>
@Html.DisplayNameFor(model => model.WildType)
</th>
<th>
@Html.DisplayNameFor(model => model.WildTypeAllele)
</th>
<th>
@Html.DisplayNameFor(model => model.AtRiskAllele)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.GeneCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.GeneName)
</td>
<td>
@Html.DisplayFor(modelItem => item.GeneComments)
</td>
<td>
@Html.DisplayFor(modelItem => item.WildType)
</td>
<td>
@Html.DisplayFor(modelItem => item.WildTypeAllele)
</td>
<td>
@Html.DisplayFor(modelItem => item.AtRiskAllele)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { }) |
@Html.ActionLink("Details", "Details", new { }) |
@Html.ActionLink("Delete", "Delete", new { })
</td>
</tr>
}
</table>
您应该这样做:
模型:
public class ModelB
{
public int Age { get; set; }
public string Name { get; set; }
}
控制器:
public ActionResult MyAction()
{
var model = new List<ModelB>
{
new ModelB{Age = 2, Name = "Bob"},
new ModelB{Age = 7, Name = "Sam"},
};
return View(model);
}
[HttpPost]
public ActionResult MyAction(List<ModelB> model)
{
//whatever
}
视图:
@model List<TestWebApplication.Models.ModelB>
...
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
Age: @Html.EditorFor(modelItem => Model[i].Age);
Name: @Html.EditorFor(modelItem => Model[i].Name);
<br />
}
<input type="submit"/>
}
请注意,我用的替代的foreach。 当您填充表单时,您不应该使用foreach-它不能很好地呈现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.