using the JQuery sortable, and trying to send the new order back to my controller, but not having a whole lot of luck. My view is:
using (Ajax.BeginForm("EditTickerOrder", new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", }))
{
<div id="editableticker">
@Html.HiddenFor(m => m.ProjectGUID)
<ul id="sortablediv">
@foreach (DGI.CoBRA.Tools.BussinessObjects.CollabLibrary.TickerObjects.Ticker t in Model)
{
<li class="ui-state-default" id="@t.pKeyGuid.ToString()">
<p>@Html.CheckBox(t.pKeyGuid.ToString(), t.Display, new { @class = "activechk" })
<span style="font-weight: bold">
@t.Text
</span>
</p>
</li>
}
</ul>
<input type="submit" value="Save New Ticker Order" />
}
and my controller is:
[HttpPost]
public ActionResult EditTickerOrder(Guid ProjectGUID, List<string> items)
{
TickerCollectionModel TickerData = new TickerCollectionModel();
TickerData.ProjectGUID = ProjectGUID;
TickerData.ListAllBySession(ProjectGUID);
return PartialView("TickerList", TickerData);
}
yet the list<string> items is always null. Any ideas?
You are writing
foreachloops, most definitely violating the naming conventions for your form input fields that the default model binder expects for working with collections. If you don’t respect the established wire format, you cannot expect the default model binder to be able to rehydrate your models in the POST action.In fact, why don’t you use view models and editor templates? They make everything trivial in ASP.NET MVC.
So let’s define a view model that will reflect your view requirements (or at least those shown in your question => you could of course enrich it with additional properties that you want to handle):
and then a controller whose responsibility is to query your DAL layer, retrieve a domain model, map the domain model into the view model we defined for this view and pass the view model to the view. Inversely, the POST action receives a view model from the view, maps the view model back into some domain model, passes the domain model to your DAL layer for processing and renders some view or redirects to a success action:
a view (it is worth noting that in this example I have used a standard form instead of AJAX but it is trivial to convert it into an AJAX form):
and finally the corresponding editor template which will automatically be rendered for each element of the Tickers collection (
~/Views/Home/EditorTemplates/TickerViewModel.cshtml):