天冬氨酸:净MVC 3:@ Html.EditorFor我在模板模型的一个子集? [英] Asp:net MVC 3: @Html.EditorFor a subcollection of my model in a template?
问题描述
我已经被困一个很长的时间来修改我的模型的一个子集,模型的集合来了空。
I've been stuck a long time to edit a subcollection of my model, the collection of the model was coming null.
我终于找到了一个解决办法,但我觉得有点脏的:
I finally found a solution, but I find it a little dirty:
首先我的测试DATAS:
First my tests datas:
Model对象
public class ContainerObject
{
public String Title { get; set; }
public List<ContainedObject> ObjectList { get; set; }
}
分集合对象
public class ContainedObject
{
public int Id { get; set; }
public String Text { get; set; }
public Boolean IsSelected { get; set; }
}
这生成目标控制方法
public ActionResult TestForm()
{
return View(new ContainerObject()
{
Title = "This is a sample title",
ObjectList = new List<ContainedObject>()
{
new ContainedObject(){Id=1, IsSelected = true, Text="ObjectOne"},
new ContainedObject(){Id=2, IsSelected = false, Text="ObjectTwo"},
new ContainedObject(){Id=3, IsSelected = true, Text="ObjectThree"},
new ContainedObject(){Id=4, IsSelected = false, Text="ObjectFour"},
}
});
}
控制器,收到编辑的对象
[HttpPost]
public ActionResult TestFormResult(ContainerObject filledObject)
{
return View();
}
视图
@model WebTestApplication.Models.ContainerObject
@{
ViewBag.Title = "TestForm";
}
@using (Html.BeginForm("TestFormResult","Home", FormMethod.Post)){
@Html.EditorFor(x => x.Title)
Html.RenderPartial("ContainedObject", Model.ObjectList);
<input type="submit" value="Submit"/>
}
局部视图(ContainedObject.cshtml)
@model IEnumerable<WebTestApplication.Models.ContainedObject>
@{
ViewBag.Title = "ContainedObject";
int i = 0;
}
@foreach (WebTestApplication.Models.ContainedObject currentObject in Model)
{
<br />
@Html.Label(currentObject.Text);
@Html.CheckBox("ObjectList[" + i + "].IsSelected", currentObject.IsSelected);
@Html.Hidden("ObjectList[" + i + "].Id", currentObject.Id);
@Html.Hidden("ObjectList[" + i + "].Text", currentObject.Text);
i++;
}
这是实际工作,但是我有一个问题:
This is actually working, but I've one problem:
- 我已经生成自己的名字,并指定容器对象的属性
我试图用 Html.EditorFor
,而不是 Html.RenderPartial
中的观点,问题是它生成我的名字链表。[0] .ID(具有附加。属性名和存取之间)
I tried to use Html.EditorFor
instead of Html.RenderPartial
in the view, the problem is that it generate me the name "ObjectList.[0].Id"(with a additional . between the property name and the accessor).
我还试图用仅@ Html.EditorFor在局部视图,但它创建瓦尔与对象的名称。
I also tried to use only @Html.EditorFor in the partial view, but it create vars with the name of the object.
如果我不使用任何模板,它的工作原理:
If I don't use any template, it works:
@model WebTestApplication.Models.ContainerObject
@{
ViewBag.Title = "TestForm";
}
@using (Html.BeginForm("TestFormResult", "Home", FormMethod.Post))
{
@Html.EditorFor(x => x.Title)
for (int i = 0; i < Model.ObjectList.Count; i++)
{
<br />
@Html.Label(Model.ObjectList[i].Text);
@Html.CheckBoxFor(m => Model.ObjectList[i].IsSelected);
@Html.HiddenFor(m => Model.ObjectList[i].Id);
@Html.HiddenFor(m => Model.ObjectList[i].Text);
}
<br /><input type="submit" value="Submit"/>
}
但在这里它是一个简单的模板,但在我的真实情况,我将有更多的数据,这将被重新使用的多个时间。那么,什么是我最好的选择?
But here it's a simple template, but in my real case, I will have much more data, and this will be re-used multiple time. So what is my best option?
推荐答案
您可以简化您code。通过引入EditorTemplate。方法如下:
You can simplify your code by introducing the EditorTemplate. Here is how:
- 在主视图保持pretty的大致相同,除了我们替换的RenderPartial与EditorFor:
TestForm.cshtml
@model WebTestApplication.Models.ContainerObject
@{
ViewBag.Title = "TestForm";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("TestFormResult", "Home", FormMethod.Post)) {
@Html.EditorFor(m => m.Title)
@Html.EditorFor(m => m.ObjectList);
<input type="submit" value="Submit" />
}
- 然后下创建一个文件夹名为 EditorTemplates 的查看/首页的(假设你的控制器主页):
- Then create a folder named EditorTemplates under Views/Home (assuming your controller is Home):
- 并添加下面的模板中的 ContainedObject 的:
ContainedObject.cshtml
@model WebTestApplication.Models.ContainedObject
<p>
@Html.DisplayFor(m => m.Text)
@Html.CheckBoxFor(m => m.IsSelected)
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.Text)
</p>
,编辑器会自动通过物体渲染视图为每个列表循环。希望它能帮助。
The editor will automatically iterate through the list of objects rendering the view for each of them. Hope it helps.
这篇关于天冬氨酸:净MVC 3:@ Html.EditorFor我在模板模型的一个子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!