我知道你們大多數人會建議我應該使用特定於我使用的窗體的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