loki
loki

Reputation: 2966

How to fill IEnumerable<T> after HttpPost request mvc 3?

i want to make a form using below code i developed Model for View :

Upvotes: 0

Views: 406

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Please read the following blog post to better understand how the model binding works for collections and how your input fields should be named: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

OK, now let's get rid of this foreach loop and use editor templates, shall we?

<table style="width:65%; vertical-align:top" id="sample">
    <%= Html.EditorFor(x => x.Properties) %>
</table>

and now define an editor template that will automatically be rendered for each element of the properties collection (~/Views/Shared/EditorTemplates/PropertyModel.ascx):

<%@ Control 
    Language="C#" 
    AutoEventWireup="true" 
    Inherits="System.Web.Mvc.ViewUserControl<PropertyModel>" 
%>
<tr>
    <td>
        <%= Html.LabelFor(x => x.ParameterName) %>
    </td>
    <td>:</td>
    <td>
        <%= Html.TextBoxFor(x => x.ParameterName) %>
    </td>
</tr>

As far as those radio buttons are concerned, there's something wrong in your design about them. They are not part of the collection model but part of the main view model and yet you are putting them inside the foreach loop that is rendered for each element of the collection property. You might need to rethink what you are trying to do here.

Upvotes: 2

Related Questions