1

Functionality: i want to update viewModel from jquery each time user choose to update PersonList on the view.

So if i have a class person :

public class Person
{
 public string Name {get;set;}
 public int Age {get;set;}
}

And i have a complex viewmodel which contain list of persons in the view i run :

foreach(var person in model.Item.PersonList)
{
 @Html.Partial("_PersonList", person)
}

Partial view :

//_PersonList partial
@model Person
@using (Html.BeginCollectionItem("Item.PersonList"))
{
 //hidden fields just for testing
 @Html.HiddenFor(x => Model.Name)
 @Html.HiddenFor(x => Model.Age )                 
}

each time user adds new person, i send ajax request and asp for the partialView(_PersonList) which already have to be populated with person data.

Age and name fields inside partial, after ajax request ends like this:

<input data-val="true" data-val-required="" id="item_PersonList_1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36__Name" name="item.PersonList[1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36].Name" type="hidden" value="somRandomName">

this works fine, however if user choose to add 20 person, then i have to do 20 requests to the server, which i want to avoid.

Thats why i tryed to send list of persons to the partialview.

I changed partialView :

//_PersonList partial
@model List<Person>
@using (Html.BeginCollectionItem("Item.PersonList"))
{
 @for (var i; i < Model.Count(); i++)
 {
   //hidden fields just for testing
   @Html.HiddenFor(x => Model[i].Name)
   @Html.HiddenFor(x => Model[i].Age )                 
 }
}

But now input fields after i get the list looks like this :

<input data-val="true" data-val-required="" id="item_PersonList_1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36_0_Name" name="item.PersonList[1dc2189b-0cc9-4397-aa6d-8a6fa1cc4e36][0].Name" type="hidden" value="somRandomName">

Which is not recognized as a part of viewmodel when i post form. so the problem is that there is added a [0] from nowhere to the name and id attributes

how can i prevent this behavior?

If i use foreach id looks like: item.Products[4456a32b-eb06-4665-be23-31666d72824e].item.Uid

joey8oro
  • 97
  • 1
  • 3
  • 14
Timsen
  • 4,066
  • 10
  • 59
  • 117
  • You mixing up 2 different methods of generating collections. The `for` loop with create consecutively numbered indexers which is fine if you are not dynamically adding and or deleting items in the view. `BeginCollectionItem` is used for generating the indexer using a `Guid` and adding a hidden input with `name="Index" value="theGuild"`. Are you trying to add 20 'blank' items to the view in a single ajax call? –  Feb 15 '15 at 22:33
  • im trying to add 20 populated items on a single ajax call, which i can inject into the html, and on post of the Main form, those 20 persons must be inside the Main forms viewModel – Timsen Feb 15 '15 at 22:37
  • If you have already rendered the existing items in the view, why does your ajax call need to get them again. Surely you ajax call is for adding a new object to the DOM so the user can create/edit it. –  Feb 15 '15 at 22:50
  • Will it be better to render it through inline jQuery? – Timsen Feb 15 '15 at 22:56
  • I'm a little confused exactly what you doing. Not sure, but [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) may help and it shows an an option of using client side code without an ajax call to generate new items –  Feb 15 '15 at 23:01
  • what i want to achieve, is that on close of a modal popup where user choose some people, i want to load those chosen people into Main form with specific layout/html, at the same time i want "merge/bind" those people to the Main forms viewModel. so on popup windows i have a Person List, and on main/parent form i have MainForm.PersonList which i want to bind to popup PersonList when modal closes. – Timsen Feb 15 '15 at 23:13
  • 1
    Without seeing more code, hard to be sure, but the issue is that you cant use both `BeginCollectionItem` and `for` because you are both methods are adding indexers. If our using `BeginCollectionItem` then you first code snippet using the `foreach` is the correct approach –  Feb 15 '15 at 23:43

0 Answers0