I want to pass an input model from a partial view to a controller. I’m rather new to MVC so still trying to understand how the default model binder works.
Via AJAX (listBox) a controller passes back a partial view and inserts into table id=searchResults.
@model ViewModels.LocationViewModel
@using (Ajax.BeginForm("ProcessSearch", "SearchR", new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "searchResults",
}))
{
<div>@Html.ListBoxFor(xxx)</div>
<input id="Search" type="submit" value="Search" />
}
Here is the controller and ViewModel that populates the partial view
public class OrderViewModel
{
public string Description { get; set; }
public string Status { get; set; }
}
public ActionResult ProcessSearch(SearchViewModel search)
{
select new OrderViewModel{
Status=f.STATUS,
Description=f.DESCRIPTION}).ToList();
return PartialView(model);
}
In the same main view I have this form that I want I want to bind to yet another view model. I simply don’t understand how to implement the default binder from the model of the partial view. I apologize if I didn’t explain this correctly. I hope it makes sense.
@using (Html.BeginForm("Testing", "SearchR", FormMethod.Post))
{
<div>@Html.DropDownListFor(yyy)</div>
<input id="Reshop" type="submit" value="Reshop" />
}
<table id="searchResults"></table>
public ActionResult Testing(RSOrderViewModel rOrder)
{
return Content("hey");
}
public class RSOrderViewModel
{
public string yyy { get; set; }
public IEnumerable<OrderViewModel> sovm { get; set; }
}
@model List<ViewModels.OrderViewModel>
@{ViewData.TemplateInfo.HtmlFieldPrefix = "sovm";
}
<table id="searchResults">
<tr>
<th>Order Id</th>
<th>Order Detail</tr>
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(x => x[i].OrderId)
</td>
<td>
@Html.DisplayFor(x => x[i].OrderDetail)
</td>
</tr>
}
</table>
The table is outside the second form. So when you POST to the
Testingaction all that is sent to the controller is the value of the dropdown list. If you want to send the collection that’s stored in this table you will have to either use AJAX or put the table inside the form:Now of course putting the table inside the form doesn’t mean that it will send anything to the server when you submit the form. You need to put input fields (hidden if you don’t want them to be visible to the user) that will contain the values that will be POSTed back. Also those input field names must follow the standard convention for binding to a list.
You haven’t actually shown how does the partial view look like but here’s an example of how it might look so that the convention is respected. For example let’s suppose that you have 2 properties in your OrderViewModel that you want to be bound: OrderId and OrderDetail:
and then you could have an editor template which will be rendered for each element of the model (
~/Views/Shared/EditorTemplates/OrderViewModel.cshtml):The name of the template is the name of the type used in the collection you want to bind to (
IEnumerable<OrderViewModel> sovm { get; set; }=>OrderViewModel.cshtml). It must also be placed inside the~/Views/Shared/EditorTemplatesfolder if it can be reused between multiple controllers or if it is specific to the current controller inside the~/Views/XXX/EditorTemplatesfolder where XXX is the current controller.