我有一个自定义类
public class BloggerViewModel
{
public Person Blogger;
public List<BloggerWebsite> BloggerWebsites;
}
我传入视图
[HttpGet]
public ActionResult Edit(int id)
{
blogger = GetById(id);
var WebSites = GetBloggersWebsites(Id);
var BloggerViewModel = new BloggerViewModel();
BloggerViewModel.Blogger = blogger;
BloggerViewModel.BloggerWebsites = WebSites;
return View(BloggerViewModel);
}
然后当我回到编辑动作
时[HttpPost]
public ActionResult Edit(BloggerViewModel entity)
{
return View(entity);
}
entity
为空。
我的观点是这样的(我拿出了你不需要看的所有代码。比如大多数文本框绑定)
@model Bloginect.Model.Models.BloggerViewModel
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
@Html.HiddenFor(model => model.Blogger.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Blogger.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Blogger.FirstName)
@Html.ValidationMessageFor(model => model.Blogger.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Blogger.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Blogger.LastName)
@Html.ValidationMessageFor(model => model.Blogger.LastName)
</div>
<div class="editor-field">
@if (Model.BloggerWebsites[0].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
@Html.TextBox("Website1")
}
</div>
<div class="editor-field">
@if (Model.BloggerWebsites[1].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
@Html.TextBox("Website2")
}
</div>
<div class="editor-field">
@if (Model.BloggerWebsites[2].Website != null)
{
Html.EditorFor(model => model.BloggerWebsites[0].Website);
}
else
{
@Html.TextBox("Website3")
}
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我做错了吗?我已经检查了其他一些类似问题的答复,他们没有回答我的问题。
答案 0 :(得分:0)
您必须使用编辑器来处理嵌套模型属性的绑定,可以找到关于如何执行此操作的一个很好的示例{。{3}}。