另一个模型绑定问题
我昨天问过类似的问题 这里稍作修改。
这是我的模型
public class InfoModel
{
public NameModel Name { get; set; }
public string Phone { get; set; }
}
public class NameModel
{
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<SelectListItem> Titles
{
get
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem() { Text = "Mr.", Value = "Mr." });
list.Add(new SelectListItem() { Text = "Mrs.", Value = "Mrs." });
list.Add(new SelectListItem() { Text = "Ms.", Value = "Ms." });
return list;
}
}
public NameModel()
{
}
public NameModel(string first, string last)
{
this.FirstName = first;
this.LastName = last;
}
}
然后我有我的第一个视图 ShowName.cshtml 如下所示
@model MyTestApp.Models.NameModel
@Html.DropDownListFor(m => m.Title, Model.Titles, Model.Titles)
<br />
@Html.LabelFor( m => m.LastName)
@Html.TextBoxFor( m => m.LastName)
<br />
@Html.LabelFor( m => m.FirstName)
@Html.TextBoxFor( m => m.FirstName)
上面的视图在 ShowInfo.cshtml 中使用如下
@model MyTestApp.Models.InfoModel
@using (Html.BeginForm())
{
@Html.Partial("ShowName", Model.Name)
<br />
@Html.LabelFor(m => m.Phone)
@Html.TextBoxFor(m => m.Phone)
<br />
<input type="submit" value="Submit Info" />
}
当用户提交任何信息时,将调用以下控制器方法
[HttpPost]
public ActionResult ShowInfo(InfoModel model)
{
...
}
问题是电话很好,但名称为空。如果我将 @Html.Partial("ShowName", Model.Name)
的调用更改为 @Html.EditorFor(m => m.Name, "ShowName")
然后标题的下拉列表显示为编辑框
Its similar question that I asked yesterday here with a slight modification.
Here are my models
public class InfoModel
{
public NameModel Name { get; set; }
public string Phone { get; set; }
}
public class NameModel
{
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<SelectListItem> Titles
{
get
{
var list = new List<SelectListItem>();
list.Add(new SelectListItem() { Text = "Mr.", Value = "Mr." });
list.Add(new SelectListItem() { Text = "Mrs.", Value = "Mrs." });
list.Add(new SelectListItem() { Text = "Ms.", Value = "Ms." });
return list;
}
}
public NameModel()
{
}
public NameModel(string first, string last)
{
this.FirstName = first;
this.LastName = last;
}
}
Then I have my first view ShowName.cshtml as follows
@model MyTestApp.Models.NameModel
@Html.DropDownListFor(m => m.Title, Model.Titles, Model.Titles)
<br />
@Html.LabelFor( m => m.LastName)
@Html.TextBoxFor( m => m.LastName)
<br />
@Html.LabelFor( m => m.FirstName)
@Html.TextBoxFor( m => m.FirstName)
the above view is used in ShowInfo.cshtml as below
@model MyTestApp.Models.InfoModel
@using (Html.BeginForm())
{
@Html.Partial("ShowName", Model.Name)
<br />
@Html.LabelFor(m => m.Phone)
@Html.TextBoxFor(m => m.Phone)
<br />
<input type="submit" value="Submit Info" />
}
When User submit any info, follwoing controller method is called
[HttpPost]
public ActionResult ShowInfo(InfoModel model)
{
...
}
The problem is that phone is fine but name is null. If I change my call of @Html.Partial("ShowName", Model.Name)
to @Html.EditorFor(m => m.Name, "ShowName")
then the drop-down for Titles is displayed as an Editbox
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用编辑器模板。因此,不要使用
Html.Partial
,而是使用:,然后将部分移动到
~/Views/Shared/EditorTemplates/ShowName.cshtml
。这就是部分不起作用的原因:它为输入字段生成错误的名称。因此,当您编写:
这会生成以下标记:
而您需要:
为了使默认模型绑定器正常工作并在主视图模型上分配 Name 属性的 LastName 属性。
当您使用编辑器模板时,如我的示例所示,它们会自动处理关联路径并为输入元素生成正确的名称。
I would recommend you using editor templates. So instead of the
Html.Partial
use:and then move the partial to
~/Views/Shared/EditorTemplates/ShowName.cshtml
.Here's the reason why the partial doesn't work: it generates wrong names for the input fields. So when you write:
this generates the following markup:
whereas you need:
for the default model binder to work correctly and assign the LastName property of the Name property on your main view model.
When you use editor templates, as shown in my example, they automatically take care of the association paths and generate proper names for the input elements.