Reputation: 409
I'm trying to create an editor in ASP.NET MVC 2 for a list of check boxes. The collection of these checkboxes is based off of a database table. I'm having trouble getting the names to come out correctly so that the values post...
ViewModels:
public class EAF
{
// other properties
public IEnumerable<ProductViewModel> _products { get; set; }
}
public class ProductViewModel
{
public int ProductPK { get; set; }
public string ProductName { get; set; }
public bool IsSelected { get; set; }
}
Main View:
<%: Html.EditorFor(m => m._products, "ProductListTemplate") %>
Editor Template (list):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IList<EmployeeAccessForm.Models.ProductViewModel>>" %>
<fieldset>
<legend>REQUEST FOR: (select item(s) to display access form)</legend>
<table>
<tr>
<% for (int i = 0; i < Model.Count(); i++) { %>
<td>
<%: Html.EditorFor(model => model[i], "ProductCheckBox") %>
</td>
<% if (i+1 % 5 == 0) { %>
</tr>
<tr>
<% } %>
<% } %>
</tr>
</table>
</fieldset>
Editor Template (item):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EmployeeAccessForm.Models.ProductViewModel>" %>
<%: Html.HiddenFor(model => model.ProductPK)%>
<%: Html.CheckBoxFor(model => model.IsSelected)%>
<%: Html.DisplayTextFor(model => model.ProductName)%>
I get a table of checkboxes as expected, but when I view the source, the inputs all have a name like _products.[0].ProductPK
where I need it to be _products[0].ProductPK
(without the first dot). Is there a way using the helpers to get the names to come out like the latter? I can do it by writing out the HTML explicitly but wondered if I could still use the helpers and get the right naming.
Upvotes: 0
Views: 923
Reputation: 239400
The problem is coming from using an editor template for the list. Even though you didn't post it, I'm assuming in your main view you have something like:
<%: Html.EditorFor(m => m._products, "ProductListTemplate") %>
This results in a prefix being added of _products
. Then in your list template, the field names there are being bound in the form of [0].ProductPK
. So the result is _products.[0].ProductPK
.
Instead in your main view you should be doing:
<fieldset>
<legend>REQUEST FOR: (select item(s) to display access form)</legend>
<table>
<tr>
<% for (int i = 0; i < Model.Count(); i++) { %>
<td>
<%: Html.EditorFor(model => model._products[i], "ProductCheckBox") %>
</td>
<% if (i+1 % 5 == 0) { %>
</tr>
<tr>
<% } %>
<% } %>
</tr>
</table>
</fieldset>
In other words, remove that middle layer that's screwing up the field names. Since the expression being passed here model._products[i]
, your field names will be prefixed with _products[0]
.
If your goal with the list template was simply to reuse this portion of the view, you can still do that but the model you pass into should be your EAF
class, and then you'd use Html.Partial
instead of Html.EditorFor
:
<%: Html.Partial("ProductListTemplate") %>
ProductListTemplate.cshtml
@model Namespace.To.EAF
...
<%: Html.EditorFor(model => model._products[i], "ProductCheckBox") %>
Upvotes: 1