我試圖構建一個視圖來編輯MVC中的一個模型,但是當我在我的「編輯」方法中獲取編輯後的模型時,某些屬性爲null,有些具有不正確的值。MVC3模型編輯 - 返回到控制器的不正確/缺失值
我已經把它分解成一個非常基本的例子,但我仍然無法弄清楚發生了什麼。當我編輯我的模型並單擊保存時,控制器中返回的模型具有錯誤的布爾值(它始終爲false)。此外,我的父母和IList對象始終爲空。真的,我永遠不想編輯這些字段,在這個模型上我只想編輯Name和Active屬性。然而,它被返回爲null,並且我的連貫的NHibernate SaveOrUpdate()方法試圖刪除它們。
所以我的問題是,爲什麼我的布爾字段總是返回false。爲什麼我的Parent和IList字段總是返回null?
這是我的控制器:
public ActionResult Edit(int id) {
var clientDetail = clientRepository.Get(id);
return View(clientDetail);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Client editedItem) {
try {
ValidateModel(editedItem);
if (this.ModelState.IsValid) {
clientRepository.SaveOrUpdate(editedItem);
return RedirectToAction("Details", new { id = id });
}
} catch (Exception ex) {
//Need to show some type of warning here...perhaps in a view bag.
}
return RedirectToAction("Index");
}
而我的觀點:
@model Models.Client
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.EditorForModel()
<input type="submit" value="Save" />
}
型號:
public class Client {
[Display(Name="Client ID")]
[DataType("Url")]
[DisplayFormat(DataFormatString = "../../Client/Details/{0}/")]
public virtual int Id { get; set; }
public virtual Client Parent { get; set; }
public virtual string Name { get; set; }
[Display(Name = "Active")]
public virtual bool Active { get; set; }
[ScaffoldColumn(false)]
[Editable(false)]
public virtual IList<Login> Logins { get; set; }
}
UPDATE:生成的HTML
<form action="/Client/Edit/17" method="post">
<table class="editor-table">
<tr>
<td style="width: 10em">
<div class="editor-label" style="text-align: right;">
* <label for="Id">Client ID:</label>
</div>
</td>
<td>
<div class="editor-field">
<input class="" data-val="true" data-val-number="The field Client ID must be a number." data-val-required="The Client ID field is required." id="Id" name="Id" type="text" value="17"> <span class="field-validation-valid" data-valmsg-for="Id" data-valmsg-replace="false">*</span>
</div>
</td>
</tr>
</table>
<table class="editor-table">
<tr>
<td style="width: 10em">
<div class="editor-label" style="text-align: right;">
<label for="Name">Name:</label>
</div>
</td>
<td>
<div class="editor-field">
<input class="" id="Name" name="Name" type="text" value="Dummy Client"> <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="false">*</span>
</div>
</td>
</tr>
</table>
<table class="editor-table">
<tr>
<td style="width: 10em">
<div class="editor-label" style="text-align: right;">
* <label for="Active">Active:</label>
</div>
</td>
<td>
<div class="editor-field">
<input class="check-box" type="checkbox" checked="'checked'"> <span class="field-validation-valid" data-valmsg-for="Active" data-valmsg-replace="false">*</span>
</div>
</td>
</tr>
</table><input type="submit" value="Save">
</form>
個
編輯模板:
模板:
@if (!ViewData.ModelMetadata.HideSurroundingHtml) {
<table class="editor-table">
<tr>
<td style="width: 10em">
<div class="editor-label" style="text-align: right;">
@(ViewData.ModelMetadata.IsRequired ? "*" : "")
<label for="@ViewData.ModelMetadata.PropertyName">@ViewData.ModelMetadata.GetDisplayName():</label>
</div>
</td>
<td>
<div class="editor-field">
@RenderSection("EditContent")
@Html.ValidationMessage("", "*")
</div>
</td>
</tr>
</table>
}
布爾模板:
@{
Layout = "~/Views/Shared/EditorTemplates/_Template.cshtml";
}
@section EditContent {
@if (ViewData.ModelMetadata.IsNullableValueType) {
<select class="list-box tri-state" >
<option value="@Model.HasValue ? " : "selected='selected'">Not Set</option>
<option value="@Model.HasValue && @Model.Value ? "selected='selected'">True</option>
<option value="@Model.HasValue && [email protected] ? "selected='selected'">False</option>
</select>
} else {
<input class="check-box" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" type="checkbox" @(Model ? "checked='checked'" : "") />
}
}
你的post方法不需要'id'參數,只需要Client對象就足夠了,默認的模型綁定器可能會與這個額外的參數有點混淆。 – 2011-04-01 20:50:07
@Matt - 感謝您的提示。但是這對這個問題沒有任何影響。編輯後的模型將其放入控制器中,但布爾型字段始終返回false,並且我的複雜類型將返回爲null。 – Brosto 2011-04-01 20:53:53
你可以顯示由視圖生成的HTML嗎?它看起來像'EditorForModel'沒有按預期生成表單字段。 – 2011-04-01 20:58:08