How to do a bulk persist on a partial view?

I would recommend you to always use editor templates. The reason your code doesn't work is because you used a partial and this partial doesn't inherit the parent template context which means that helpers that are used inside it will generate wrong names for the input fields.

Up vote 2 down vote favorite 2 share g+ share fb share tw.

I have written a partial view that displays a list of rows, where some fields in the row are editable using a textbox, checkbox, or whatever. I would like the parent view to have a "submit" button that posts the whole collection of updated rows so that I can do a group update rather than posting once for every updated row. Here's what I have: public class GroupModel { public string SomeGenericProperty { get; set; } public IEnumerable Items { get; set; } } public class ItemModel { public long ID { get; set; } public string Field1 { get; set; } public string Field2 { get; set; } } Then I have a view "GroupDetails": @model MyNamespace.

GroupModel ... @using (Html. BeginForm("SaveItems", "Home")) { ... @Html. Partial("ItemList", Model.

Items) ... } And a partial view "ItemList": @model IEnumerable Field 1 Field 2 @foreach (var item in Model) { @Html. TextBoxFor(modelItem => item. Field1) @Html.

TextBoxFor(modelItem => item. Field2) @Html. HiddenFor(i => item.

ID) } But when I post, the information from partial view is not being posted; the GroupModel object's Items property is null. What's the right way to do this? Asp.

Net-mvc-3 razor link|improve this question asked Jan 20 at 11:55Shaul3,55833388 91% accept rate.

I would recommend you to always use editor templates. The reason your code doesn't work is because you used a partial and this partial doesn't inherit the parent template context which means that helpers that are used inside it will generate wrong names for the input fields. For example if you look at the source code of your page you will see this: instead of the correct name which is: I would recommend you reading the following blog post to better understand the wire format that the default model binder expects.

So start by fixing your main view first and replace the partial with an editor template: @model MyNamespace. GroupModel @using (Html. BeginForm("SaveItems", "Home")) { ... Field 1 Field 2 @Html.

EditorFor(x => x. Items) ... } and then define a custom editor template that will be rendered for each element of the model (~/Views/Shared/EditorTemplates/ItemModel. Cshtml - this works by convention, it should be either inside ~/Views/Shared/EditorTemplates if you want it to be reused among controllers or inside ~/Views/Home/EditorTemplates if you want this template to be available only for the Home controller - and the name of the template must be the type of the collection - in your case it is ItemModel.

Cshtml): @model MyNamespace. ItemModel @Html. TextBoxFor(x => x.

Field1) @Html. TextBoxFor(x => x. Field2) @Html.

HiddenFor(x => x. ID) Now everything will bind fine and dandy in your controller action: HttpPost public ActionResult SaveItems(GroupModel model) { ... }.

1 Unbelievable. Speed, accuracy, clarity - it's people like you who make StackOverflow such an indispensible resource. Thank you SO MUCH!

– Shaul Jan 20 at 12:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions