I'm starting out with MVC but not sure it's the best option.
I need to create a form that is based upon a collection. Eg it might look like this:
product Price Item 1 [textbox] Item 2 [textbox]
[submit button]
where "item" is pulled from the database and textbox allows users to update the price.
essentially this is a type of datagrid but i don't want webforms style update each row one at a time i need to update the entire set of text boxes in one post.
Ideally I don't want a javascript based solution as it has to work without javascript.
Is this possible in MVC or should I stick to webforms (where I could do this in a repeater by iterating through he r开发者_Go百科epeater items on postback)
I wrote blog entry about it: ASP.NET MVC - Binding model to a list
To summarize:
For every row you have to generate inputs with proper prefixes. Sample:
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.Hidden("contacts[" + i + "].ID", item.ID)%>
<%= Html.TextBox("contacts[" + i + "].Name", item.Name)%>
</td>
<td>
<%= Html.TextBox("contacts[" + i + "].Surname",item.Surname)%>
</td>
<td>
<%= Html.TextBox("contacts[" + i + "].Phone",item.Phone)%>
</td>
</tr>
<%
i++;
} %>
And then in controller:
[HttpPost]
public ActionResult List(IEnumerable<Contact> contacts)
{
//Here we have populated contact list, contacs parameter is filled with data from form. You save it here to your repository.
return RedirectToAction("List");
}
精彩评论