I created a view with a variable length list as described here: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/.
I am trying to use the PRG pattern with action filters as described at point 13 here: http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx.
I have an Edit action:
[HttpGet, ImportModelStateFromTempData]
public ActionResult Edit(int id)
{
}
And the post action:
[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 });
}
All works fine including validation and error messages.
The only problem I have is that newly added items and deleted items (client side deleted/added) are not preserved after the redirect. I am trying to find a way to update my model after the redirect with the new items. I changed the ImportModelStateFromTempData attribute to use the OnActionExecuting override instead of the OnActionExecuted override to have the ModelState available in the action but I don't see a clean way to update my model from the passed in ModelState.
Changed 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);
}
}
Any input on this is much appreciated, thanks.
Harmen
UPDATE: Thought I might add some more of my (pseudo) code to make it more clear:
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;
}
My Edit view:
@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>
Editor template for the 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>