[英]MVC3 Model Editing - incorrect/missing values returning to controller
我试图建立一个视图以在MVC中编辑我的模型之一,但是当我在“编辑”方法中获得编辑后的模型时,某些属性为null,而某些属性值不正确。
我已经将其简化为一个非常基本的示例,但是我仍然不知道发生了什么。 当我编辑模型并单击“保存”时,控制器中返回的模型具有错误的布尔值(始终为false)。 另外,我的Parent和IList <>对象始终为null。 真的,我永远都不想编辑这些字段,在此模型上,我只想编辑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; }
}
更新:生成的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 && !@Model.Value ? "selected='selected'">False</option>
</select>
} else {
<input class="check-box" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" type="checkbox" @(Model ? "checked='checked'" : "") />
}
}
通过将EditorTemplate更改为简单的@ Html.CheckBox(“”,Model),我能够解决布尔字段的问题。 尽管这不适用于可为布尔值的布尔值,但就我而言,我没有任何可担心的。 我的新编辑器模板如下所示:
@{
Layout = "~/Views/Shared/EditorTemplates/_Template.cshtml";
}
@model Boolean
@section EditContent {
@Html.CheckBox("", Model)
}
以及生成的HTML:
<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 checked="checked" data-val="true" data-val-required="The Active field is required." id="Active" name="Active" type="checkbox" value="true" /><input name="Active" type="hidden" value="false" />
<span class="field-validation-valid" data-valmsg-for="Active" data-valmsg-replace="false">*</span>
</div>
</td>
</tr>
</table>
希望这可以帮助其他人避免我经历过的事情,但这绝对是一次学习的经历。 谢谢大家以上所有有用的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.