MVC结合使用列表属性模型忽略了其他属性 [英] MVC binding to model with list property ignores other properties
问题描述
我对此很复杂类型的列表属性基本视图模型。当绑定,我似乎与得到的值或者列表,或者根据公布的值(即视图布置)其他模特属性被卡住。
视图模型:
公共类MyViewModel
{
公众诠释标识{搞定;组; }
公共字符串Property1 {搞定;组; }
公共字符串Property2 {搞定;组; } 公开名单< MyDataItem>数据{搞定;组; }
}公共类MyDataItem
{
公众诠释标识{搞定;组; }
公众诠释的ParentId {搞定;组; }
公共字符串名称{;组; }
公共字符串值{获得;组; }
}
控制器操作:
公众的ActionResult MyForm的()
{
MyViewModel模式=新MyViewModel(); model.Id = 1;
model.Data =新的List< MyDataItem>()
{
新MyDataItem {ID = 1的ParentId = 1,名称=MyListItem1,值=someValue中}
}; 返回查看(模型);
} [HttpPost]
公众的ActionResult MyForm的(MyViewModel模型)
{
// ... 返回查看(模型);
}
下面是基本视图(无单加价)
@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>我视图模型< /传说> @ Html.HiddenFor(型号=> model.Id) < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Property1)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Property1)
@ Html.ValidationMessageFor(型号=> model.Property1)
< / DIV> < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Property2)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Property2)
@ Html.ValidationMessageFor(型号=> model.Property2)
< / DIV> &所述p为H.;
<输入类型=提交值=保存/>
&所述; / P>
< /字段集>
}
当调回控制器,我得到了2属性值和预期数据属性的空值。
如果我添加了标记为列表如下(根据此信息<一个href=\"http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx\">Scott Hanselman的帖子和菲尔哈克的帖子):
&LT; DIV CLASS =主编场&GT;
@for(INT I = 0; I&LT; Model.Data.Count();我++)
{
MyDataItem数据= Model.Data [I]
@ Html.Hidden(model.Data [+ I +] .ID,data.Id)
@ Html.Hidden(model.Data [+ I +] .ParentIddata.ParentId)
@ Html.Hidden(model.Data [+ I +] .Name点,data.Name)
@ Html.TextBox(model.Data [+ I +] .value的data.Value)
}
&LT; / DIV&GT;
该模型的数据属性绑定成功,但其他属性为null。
贴在表格值如下:
<$p$p><$c$c>Id=1&Property1=test1&Property2=test2&model.Data%5B0%5D.Id=1&model.Data%5B0%5D.ParentId=1&model.Data%5B0%5D.Name=MyListItem1&model.Data%5B0%5D.Value=SomeValue有没有办法让两套填充性能还是我只是缺少明显的东西?
编辑:
对于那些谁是好奇。根据来自 MartinHN 了答案,原来产生的加价是:
&LT; DIV CLASS =主编场&GT;
&LT;输入ID =model_Data_0__IdNAME =model.Data [0] .ID类型=隐藏值=1/&GT;
&LT;输入ID =model_Data_0__ParentIdNAME =model.Data [0] .ParentId类型=隐藏值=1/&GT;
&LT;输入ID =model_Data_0__NameNAME =model.Data [0] .Name点类型=隐藏值=MyListItem1/&GT;
&LT;输入ID =model_Data_0__ValueNAME =model.Data [0] .value的类型=文本VALUE =someValue中/&GT;
&LT; / DIV&GT;
新产生的加价是:
&LT; DIV CLASS =主编场&GT;
&LT;输入ID =Data_0__Id数据-VAL =真NAME =数据[0] .ID类型=隐藏值=1数据-VAL-数=ID字段必须是数字。 数据-VAL-所需=是必需的ID字段。 /&GT;
&LT;输入ID =Data_0__ParentIdNAME =数据[0] .ParentId类型=隐藏值=1数据-VAL =真正的数据-VAL-数=这个领域的ParentId必须是数字。 数据-VAL-所需=是必需的的ParentId场。 /&GT;
&LT;输入ID =Data_0__NameNAME =数据[0] .Name点类型=隐藏值=MyListItem1/&GT;
&LT;输入ID =Data_0__ValueNAME =数据[0] .value的类型=文本VALUE =someValue中/&GT;
&LT; / DIV&GT;
这导致如下贴值:
<$p$p><$c$c>Id=1&Property1=test1&Property2=test2&Data%5B0%5D.Id=1&Data%5B0%5D.ParentId=1&Data%5B0%5D.Name=MyListItem1&Data%5B0%5D.Value=SomeValue注意有没有'的模式。在名称和发布值...
尝试改变code的数据收集到这一点,并让MVC照顾命名的:
&LT; DIV CLASS =主编场&GT;
@for(INT I = 0; I&LT; Model.Data.Count();我++)
{
@ Html.HiddenFor(M = GT; m.Data [I] .ID)
@ Html.HiddenFor(M = GT; m.Data [I] .ParentId)
@ Html.HiddenFor(M = GT; m.Data [I] .Name点)
@ Html.TextBoxFor(M = GT; m.Data [I]。价值)
}
&LT; / DIV&GT;
I have a basic ViewModel with a property that is a List of complex types. When binding, I seem to be stuck with getting either the list of values, OR the other model properties depending on the posted values (i.e. the view arrangement).
The view model:
public class MyViewModel
{
public int Id { get; set; }
public string Property1 { get; set; }
public string Property2 { get; set; }
public List<MyDataItem> Data { get; set; }
}
public class MyDataItem
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
The controller actions:
public ActionResult MyForm()
{
MyViewModel model = new MyViewModel();
model.Id = 1;
model.Data = new List<MyDataItem>()
{
new MyDataItem{ Id = 1, ParentId = 1, Name = "MyListItem1", Value = "SomeValue"}
};
return View(model);
}
[HttpPost]
public ActionResult MyForm(MyViewModel model)
{
//...
return View(model);
}
Here is the basic view (without the list mark-up)
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>My View Model</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Property1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Property1)
@Html.ValidationMessageFor(model => model.Property1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Property2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Property2)
@Html.ValidationMessageFor(model => model.Property2)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
When posted back to the controller, I get the 2 property values and a null value for the 'Data' property as expected.
If I add the mark-up for the List as follows (based on the information in this Scott Hanselman post and Phil Haack's post):
<div class="editor-field">
@for (int i = 0; i < Model.Data.Count(); i++)
{
MyDataItem data = Model.Data[i];
@Html.Hidden("model.Data[" + i + "].Id", data.Id)
@Html.Hidden("model.Data[" + i + "].ParentId", data.ParentId)
@Html.Hidden("model.Data[" + i + "].Name", data.Name)
@Html.TextBox("model.Data[" + i + "].Value", data.Value)
}
</div>
The 'Data' property of the model is successfully bound but the other properties are null.
The form values posted are as follows:
Id=1&Property1=test1&Property2=test2&model.Data%5B0%5D.Id=1&model.Data%5B0%5D.ParentId=1&model.Data%5B0%5D.Name=MyListItem1&model.Data%5B0%5D.Value=SomeValue
Is there a way to get both sets of properties populated or am I just missing something obvious?
EDIT:
For those of you who are curious. Based on the answer from MartinHN, the original generated mark-up was:
<div class="editor-field">
<input id="model_Data_0__Id" name="model.Data[0].Id" type="hidden" value="1" />
<input id="model_Data_0__ParentId" name="model.Data[0].ParentId" type="hidden" value="1" />
<input id="model_Data_0__Name" name="model.Data[0].Name" type="hidden" value="MyListItem1" />
<input id="model_Data_0__Value" name="model.Data[0].Value" type="text" value="SomeValue" />
</div>
The new generated mark-up is:
<div class="editor-field">
<input id="Data_0__Id" data-val="true" name="Data[0].Id" type="hidden" value="1" data-val-number="The field Id must be a number." data-val-required="The Id field is required." />
<input id="Data_0__ParentId" name="Data[0].ParentId" type="hidden" value="1" data-val="true" data-val-number="The field ParentId must be a number." data-val-required="The ParentId field is required." />
<input id="Data_0__Name" name="Data[0].Name" type="hidden" value="MyListItem1" />
<input id="Data_0__Value" name="Data[0].Value" type="text" value="SomeValue" />
</div>
Which results in the following posted values:
Id=1&Property1=test1&Property2=test2&Data%5B0%5D.Id=1&Data%5B0%5D.ParentId=1&Data%5B0%5D.Name=MyListItem1&Data%5B0%5D.Value=SomeValue
Notice there's no 'model.' in the name and posted values...
Try to change the code for the Data collection to this, and let MVC take care of the naming:
<div class="editor-field">
@for (int i = 0; i < Model.Data.Count(); i++)
{
@Html.HiddenFor(m => m.Data[i].Id)
@Html.HiddenFor(m => m.Data[i].ParentId)
@Html.HiddenFor(m => m.Data[i].Name)
@Html.TextBoxFor(m => m.Data[i].Value)
}
</div>
这篇关于MVC结合使用列表属性模型忽略了其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!