我創建了一個可變長度列表的視圖,如下所述:http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/。MVC 3.0編輯可變長度列表並使用PRG模式
我正在嘗試使用帶有動作過濾器的PRG模式,如第13點所述:http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx。
我有一個編輯動作:
[HttpGet, ImportModelStateFromTempData]
public ActionResult Edit(int id)
{
}
和後動作:
[HttpPost, ExportModelStateToTempData]
public ActionResult Edit(int id, FormCollection formCollection)
{
if (!TryUpdateModel<CategoryEntity>(category, formCollection))
{
return RedirectToAction("Edit", new { id = id });
}
// succes, no problem processing this...
return RedirectToAction("Edit", new { id = id });
}
一切工作正常,包括驗證和錯誤消息。
我唯一的問題是新增項目和刪除項目(客戶端刪除/添加)在重定向後不會保留。我試圖找到一種方式來更新我的模型後重新導入新項目。我將ImportModelStateFromTempData屬性更改爲使用OnActionExecuting覆蓋而不是OnActionExecuted覆蓋,以便在操作中使用ModelState,但我沒有看到從傳入的ModelState中更新模型的乾淨方法。
更改ImportModelStateFromTempData:
public class ImportModelStateFromTempData : ModelStateTempDataTransfer
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;
if (modelState != null)
{
filterContext.Controller.ViewData.ModelState.Merge(modelState);
}
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;
//if (modelState != null)
//{
// //Only Import if we are viewing
// if (filterContext.Result is ViewResult)
// {
// filterContext.Controller.ViewData.ModelState.Merge(modelState);
// }
// else
// {
// //Otherwise remove it.
// filterContext.Controller.TempData.Remove(Key);
// }
//}
base.OnActionExecuted(filterContext);
}
}
在此的任何輸入是非常讚賞,謝謝。
Harmen
更新:想我可能會添加一些更多的我的(僞)代碼,以使其更清晰:
public class CategoryEntity
{
public int Id;
public string Name;
public IEnumerable<CategoryLocEntity> Localized;
}
public class CategoryLocEntity
{
public int CategoryId;
public int LanguageId;
public string LanguageName;
public string Name;
}
我編輯觀點:對於
@model CategoryEntity
@{
ViewBag.Title = Views.Category.Edit;
}
<h2>@Views.Category.Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript"><!--
$(document).ready(function() {
$('#addItem').click(function() {
var languageId = $('#languageId').val();
var index = $('#editor-rows').children().size() - 1;
$.ajax({
url: this.href + '?languageId=' + languageId + '&index=' + index,
cache: false,
error: function (xhr, status, error) {
alert(error);
},
success: function (html) {
$('#editor-rows').append(html);
}
});
return false;
});
$("a.removeItem").live("click", function() {
$(this).parents("div.editor-row:first").remove();
return false;
});
});
--></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
<fieldset>
<legend>@Views.Shared.Category</legend>
@Html.HiddenFor(model => model.Id)
<div id="editor-rows">
<div class="editor-row">
<div class="editor-label">
@Html.LabelFor(model => model.Name, Views.Shared.NameEnglish)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
@for (int i = 0; i < Model.Localized.Count; i++)
{
@Html.EditorFor(m => m.Localized[i], "_CategoryLoc", null, null)
}
</div>
<div class="editor-label"></div>
<div class="editor-field">
@Html.DropDownList("languageId", (IEnumerable<SelectListItem>)ViewBag.LanguageSelectList)
@Html.ActionLink(Views.Category.AddNewLanguage, "AddNewLanguage", null, new { id = "addItem" })
</div>
<p class="clear">
<input type="submit" value="@Views.Shared.Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink(Views.Shared.BackToList, "Index")
</div>
編輯模板CategoryLocEntity:
@model CategoryLocEntity
<div class="editor-row">
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.LanguageId)
<div class="editor-label">
@Html.LabelFor(model => model.LanguageName, Model.LanguageName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
<a href="#" class="removeItem">@Views.Shared.Remove</a>
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
您好,感謝的解決方案,但是它不工作,我的情況,因爲我的收藏指數是不連續的(我使用額外的.index字段來這樣做)。 – hazzik 2012-10-06 21:24:17