Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelbinding IEnumerable in ASP.NET MVC POST?

Is there any issues with modelbinding IEnumerable types to an MVC POST?

Some properties in my Model are not being bound upon a post to an action. Seems that properties on the model like strings are ok, but my IEnumerable is what is not being bound.

Here's a snippet of my code:

<%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.Test) %>
    <% for (int i = 0; i < Model.ResponseInfo.SubsetInfo.BandAvailabilities.Count(); i++)
    {%>
        <%: Html.TextBoxFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities.ToArray()[i].BandName) %>
  <% } %>

And here is what those properties look like in the model:

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }
public string Test { get; set; }

The view works fine and outputs a list of textboxes with the expected values in them. But the post Action which gets fired only recognises the Test string as a property. The model state does not contain my IEnumerable data either.

like image 928
Arkiliknam Avatar asked Jan 12 '12 09:01

Arkiliknam


People also ask

How pass IEnumerable list from controller view in MVC?

you can use ToList() method to pass Ienumerable to a model.

What is IEnumerable in MVC view?

IEnumerable will execute select query on server side, load data in-memory on client side and then filter data. IEnumerable can be used for querying data from in-memory collections like List, Array etc.

How does model binding work in MVC?

How Model Binding Works. Model binding is a simplistic way to correlate C# code with an HTTP request. The model binding applies to transforming the HTTP request data in the query's form string and form collection of the action method parameters. We can consider these parameters to be primitive type or complex type.

How do I bind a model to view in MVC core?

How does model binding work in ASP.NET Core MVC. In an empty project, change Startup class to add services and middleware for MVC. Add the following code to HomeController, demonstrating binding of simple types. Add the following code to HomeController, demonstrating binding of complex types.


1 Answers

Model binding depends upon how generated html looks like. for ur particular scenario to bind properly html should look like

<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[0].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[1].BandName"/>
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[2].BandName"/>
.
.
<input type="text" name = "ResponseInfo.SubsetInfo.BandAvailabilities[n].BandName"/>

i have not tried it but i am almost certain that call to ToArray method in loop is keeping the system from generating proper names for nested inputs. There are couple of things you can do to remedy this First, in your view model change

public IEnumerable<BandAvailabilityInfo> BandAvailabilities { get; set; }

to

public IList<BandAvailabilityInfo> BandAvailabilities { get; set; }  //or Array

so you don't have to call ToArray method in the loop and proper names are generated for inputs. Second, make an editor template and put it in Editor templates folder either under the current controller or in shared folder's Editor template folder. Make this view accept model of type BandAvailabilityInfo and name of this view should also be BandAvailabilityInfo. then in your main view you only have to replace entire loop with

 <%: Html.EditorFor(m => m.ResponseInfo.SubsetInfo.BandAvailabilities%>

and rest will be handled by framework itself

like image 65
Muhammad Adeel Zahid Avatar answered Oct 21 '22 18:10

Muhammad Adeel Zahid