0

first question on stack so please be gentle ;). I have a long MVC form that requires the user to be able to click an 'Add Person' button , which would then create a copy of an 'Add Person' partial view , which is then filled in with the Person details. On form submit, the controller would then need to contain the details of each new added person stored as a Person object in the Person[] array I have in my View Model. so for example:

User clicks 'Add Person' button 3 times
3x ' Add Person' partial views are displayed on screen, one after the other
User fills in the 3 listed forms
User submits form
Model submitted to controller contains an array of 3 Person objects, populated with the values the user has entered.

I've got the EditorFor working when displaying a list of template forms for an already populated Array of Person objects, but not sure how I would go about actually inserting a new 'Person' into the model via mouse click. Each new person will need to be given an ID of Guid type.

Sorry if my question is vague.I'm trying not to be. I cant provide sample code for my exact solution as this is for a government project but can whip up an similar example if required. Thank you for your time

Adamski343
  • 39
  • 9
  • You have not shown any code so impossible to give you an answer, but [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) shows 2 options for dynamically creating collection objects. –  Jul 17 '15 at 12:15

2 Answers2

0

This should give you a general idea of how to do it

    var partialView = '@Html.Raw(@Html.Partial("Person", new ViewModel()).ToString().Replace("\r\n", String.Empty).Replace("\n", String.Empty).Replace("\r", String.Empty))';
    partialView = partialView.replace('id="1"', 'id=ListName_{0}'.format(newId));
    $("#persons").append(partialView);

Firstly I create a variable containing the partial view as a string, next we need to change the ids and the rest of the properties so they follow the convention used for lists when data binding in MVC.

It follows the following convention

<input type="text" name="stocks[0].Key" value="MSFT" />
<input type="text" name="stocks[1].Key" value="AAPL" />

See the following for a description http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

Once all ids are correctly created you can post the form as per usual.

3dd
  • 2,520
  • 13
  • 20
  • Ah great, this was basically what I was trying to do but VS kept moaning on the .append , probably because I hadn't thought about using the @Html.Raw helper. :) thank you for taking the time to answer my query, appreciate it – Adamski343 Jul 17 '15 at 12:41
0

You're not going to be able to add a person to the view model from client side code. The view model doesn't exist client side, only server side.

My suggestion is to make sure the partial views are all in the form being submitted and have an array of Person objects as a parameter in the controller action you're posting to. You'll need to make sure you use the correct convention for each name on each input field of each partial view.

So for example the partial view each input field would need to be named like this Person[index].Field. Where index is an integer and Field is the appropriate property from the Person model that this particular input tag represents.

Casey
  • 805
  • 4
  • 10