【发布时间】:2015-03-06 13:52:36
【问题描述】:
当模型是 List 类型时,我们如何使用 lambda?
@model List<ViewModels.MyModel>
这不适用于 EditorFor
@Html.EditorFor(m=>m.FirstOrDefault().Value)
如何将模型与 EditorFor 绑定?
【问题讨论】:
标签: asp.net-mvc mvvm lambda editorfor
当模型是 List 类型时,我们如何使用 lambda?
@model List<ViewModels.MyModel>
这不适用于 EditorFor
@Html.EditorFor(m=>m.FirstOrDefault().Value)
如何将模型与 EditorFor 绑定?
【问题讨论】:
标签: asp.net-mvc mvvm lambda editorfor
您已对其进行迭代并按集合进行索引:
@for(int i =0; i <Model.Count; i ++)
{
@Html.EditorFor(m=> Model[i].Value)
}
【讨论】:
<input> 的 ID,由 EditorFor 创建。
下面的完整示例:
您的控制器代码:
public class ModelValue
{
public int ID { get; set; }
public string Name { get; set; }
}
public class TestController : Controller
{
public ActionResult Index()
{
List<ModelValue> model = new List<ModelValue>();
for (int i = 0; i < 12; i++)
{
model.Add(new ModelValue { ID = i, Name = "Name " + i.ToString() });
}
return View(model);
}
[HttpPost]
public ActionResult Index(List<ModelValue> model)
{
return View(model);
}
}
您的查看代码:
@model List<MVCApp.Controllers.ModelValue>
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Count; i++)
{
@Html.EditorFor(modelitem => Model[i].ID)
@Html.EditorFor(modelitem => Model[i].Name)
<br />
}
<button type="submit">Go</button>
}
【讨论】: