我知道你们大多数人会建议我应该使用特定于我使用的窗体的ViewModels,但我很好奇为什么我的子对象不能绑定到TryUpdateModel上。实体框架TryUpdateModel子对象?
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
@Html.HiddenFor(model => model.UserId)
@Html.HiddenFor(model => model.PrimaryAddress.AddressId)
<div class="editor-label">
@Html.LabelFor(model => model.PrimaryAddress.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PrimaryAddress.FirstName)
@Html.ValidationMessageFor(model => model.PrimaryAddress.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PrimaryAddress.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PrimaryAddress.LastName)
@Html.ValidationMessageFor(model => model.PrimaryAddress.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsApproved)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsApproved)
@Html.ValidationMessageFor(model => model.IsApproved)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.IsEmployee)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.IsEmployee)
@Html.ValidationMessageFor(model => model.IsEmployee)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
而控制器代码:
[HttpPost]
public ActionResult Edit(int id, FormCollection form)
{
var user = Token.DB.Users.Include("PrimaryAddress").Single(x => x.UserId == id);
if (TryUpdateModel(user, new string[] { "UserName", "Email", "IsApproved", "IsEmployee", "PrimaryAddress.FirstName", "PrimaryAddress.LastName" }))
{
try
{
Token.DB.SaveChanges();
return RedirectToAction("index");
}
catch (Exception ex)
{
while (ex.InnerException != null)
ex = ex.InnerException;
if (ex.Message.ToLowerInvariant().Contains("unique"))
ModelState.AddModelError("UserName", "UserName already exists");
}
}
return View(User);
}
代码不抛出任何异常,它只是不填充表单中的user.PrimaryAddress.FirstName或user.PrimaryAddress.LastName。我想知道为什么?
我已经知道我可以用特定的ViewModel解决问题并在后台映射信息。我也可以这样做:
<!-- Edit.cshtml -->
<div class="editor-field">
@Html.EditorFor(model => model.PrimaryAddress.FirstName, null, "FirstName")
@Html.ValidationMessageFor(model => model.PrimaryAddress.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PrimaryAddress.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PrimaryAddress.LastName, null, "LastName")
@Html.ValidationMessageFor(model => model.PrimaryAddress.LastName)
</div>
// UsersController.cs
if (TryUpdateModel(user, new string[] { "UserName", "Email", "IsApproved", "IsEmployee"})
&& TryUpdateModel(user.PrimaryAddress, new string[] {"FirstName", "LastName" }))
所以真正的问题是为什么它没有在第一个例子中绑定?
不知道为什么-1,明确指出我的问题,给了例子。我是否应该盲目地遵循MVC纯粹主义的观点特定模型的一切,而不理解发动机罩下面发生了什么?感谢Darin解释TryUpdateModel不支持“嵌套属性”。 – Sam