Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Multiple Nested Data in ASP.NET MVC using jQuery
    text
    copied!<p>I have been to work on an implementation where I have a base class with a collection object of an another class. IT is quite similar to the implementation found on this <a href="http://www.highoncoding.com/Articles/640_Adding_Multiple_Nested_Data_in_ASP_NET_MVC_Application.aspx" rel="nofollow">link</a>. There is one small difference between the site's implementation and mine. Since I have lots of input elements, creating them line by line will not be feasible. So I implemented a jQuery tab which loads the content from div. The div calls custom controls using Html.RenderPartial. All said and done, it works fine until this step. Using jQuery, I try to change the name property using the following code.</p> <pre><code> $('#' + id + ' input[id^="Applicant_"]').each(function() { var nameChange = this.id; var objectName = this.id.replace('Applicant_', ''); nameChange = nameChange.replace('Applicant_', 'Application.Applicants'); nameChange = nameChange.replace(objectName, ''); nameChange = nameChange + '[' + tabCounter + '].' + objectName; $(this).attr("name", nameChange); //this.id = nameChange; //this.name = nameChange; }); </code></pre> <p>Once the html is rendered on IE8, I can see the elements name unchanged, however there is a new attribute called submitName, which I am assuming is the one getting submitted. For some reason just like article I am not getting multiple elements. I am not sure what I am missing.</p> <p>Here are more details</p> <p>I am using Visual Studio 2008 with ASP.NET MVC2. The Base model is</p> <pre><code>public class Application { private IList&lt;Applicant&gt; applicants = new List&lt;Applicant&gt;(); public virtual IList&lt;Applicant&gt; ApplicantsList { get { return applicants; } set { applicants = value; } } } public class Applicant { [DisplayName("Last Name")] public virtual string LName { get; set; } [DisplayName("First Name")] public virtual string FName { get; set; } } </code></pre> <p>The main aspx contains the following code.</p> <pre><code>&lt;% Html.RenderPartial("~/Views/Shared/UserControls/Application/Edit/ContactInformation.ascx"); %&gt; </code></pre> <p>The code to change the name is already given above. The submit names are getting generated correctly the way it should. But for some reason when I do a submit to the controller action, the number of elemtnts under Applicant are always 0. I tried manually hardcoding with 3 elements and I got the same result.</p> <p>I downloaded the article and tested it and it is working as mentioned in the article. I am not sure if I am doing anything different.</p> <p>Any help would be appreciated.</p> <p><em><strong>UPDATE:</em></strong></p> <p>To make my life a little easier for testing, I hardcoded the following html into the cretae aspx page</p> <pre><code>&lt;input type="text" name="ApplicationID" value="1" /&gt; &lt;input type="text" name="Application.ApplicantsList[0].LName" value="LName-0" /&gt; &lt;input type="text" name="Application.ApplicantsList[1].LName" value="LName-1" /&gt; &lt;input type="text" name="Application.ApplicantsList[2].LName" value="LName-2" /&gt; </code></pre> <p>The code should , when submitted, have model.ApplicationID as 1 and the count for Application.ApplicantsList should be 3. The application ID is correctly getting submitted, however the collection is not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload