Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding Child Models at Runtime at the click of a link or button in ASP.NET MVC
    text
    copied!<p>I have been working on a MVC application for the past couple of weeks and I am currently stumped with a problem I have. The application has two entities</p> <ol> <li>Application </li> <li>Applicant</li> </ol> <p>The Application can have one or more Applicants. I have established the relationships in Nhibernate to reflect the same. The problem I am having is this In the Application Model I have a ISet</p> <pre><code> private ISet&lt;Applicant&gt; applicantsList = new HashedSet&lt;Applicant&gt;(); [DisplayName("Applicant Information")] public virtual ISet&lt;Applicant&gt; ApplicantsList { get { return applicantsList; } set { applicantsList = value; } } </code></pre> <p>As a temporary measure, I am creating 5 dummy elements in the controller when the first set of View is being built.</p> <pre><code> Application appToCreate = new Application(); for (int i = 0; i &lt; 5; i++) { appToCreate.ApplicantsList.Add(new Applicant(i)); } appToCreate.Partnership = ApplicationUtilities.ApplicationSolePartner.Partnership.ToString(); return View(appToCreate); </code></pre> <p>The i here indicates the index of each of the applicant. And in the main view I have the following code to generate the views.</p> <p>Generating of Tabs for JQuery</p> <pre><code> &lt;% for (int i = 0; i &lt; Model.ApplicantsList.Count ; i++) {%&gt; &lt;li&gt; &lt;a href='#tabs-&lt;%=i %&gt;'&gt;Applicant &lt;%=i %&gt; &lt;/a&gt; &lt;span class='ui-icon ui-icon-close' role='presentation'&gt;Remove Tab&lt;/span&gt; &lt;/li&gt; &lt;% } %&gt; </code></pre> <p>Generation of the Tab Content for JQuery</p> <pre><code> int iCounter = 0; foreach (var item in Model.ApplicantsList) { item.ApplicantCounterID = iCounter; %&gt; &lt;div id="tabs-&lt;%=iCounter %&gt;"&gt; &lt;div id="accordionTab-&lt;%=iCounter %&gt;"&gt; &lt;% Html.RenderPartial("~/Views/Shared/UserControls/Application/ApplicantInformation.ascx",Model.ApplicationsAsList[iCounter]); %&gt; &lt;/div&gt; &lt;/div&gt; &lt;% iCounter++; } </code></pre> <p>And in the child view, I have the following code</p> <pre><code> &lt;td&gt;&lt;%= Html.LabelFor(model =&gt; model.FName) %&gt;&lt;/td&gt; &lt;td&gt; &lt;%= Html.TextBoxFor(model =&gt; model.FName).ToString().Replace("FName", "ApplicantsList[" + Model.ApplicantCounterID + "].FName")%&gt; &lt;%= Html.ValidationMessage("ApplicantsList[" + Model.ApplicantCounterID + "].FName")%&gt; &lt;/td&gt; </code></pre> <p>I do understand that creating the nestmodels before hand is not the right approach. So I added a ActionLink / Button that will call a action in the same controller to add a new applicant object when clicked.</p> <p>Action Link</p> <pre><code>&lt;%= Html.ActionLink("Add Tab","AddTab", new {controller="Application"}) %&gt; </code></pre> <p>This does call the Addtab action in the controller but I am not able to post the loaded model so all the time the applicantslist is being reset to 0.</p> <p>Button </p> <pre><code>&lt;input type="button" value="Add Tab" onclick="location.href='&lt;%= Url.Action("AddTab", "Application") %&gt;'" /&gt; </code></pre> <p>This also does the same thing, the form model is not being submited. Here is AddTab action code</p> <pre><code> public ActionResult AddTab(Application model) { model.ApplicantsList.Add(new Applicant()); return View("CreatePartner", model); } </code></pre> <p>If I add the Post verb on the top of the method, MVC routing starts looking for a view called AddTab. </p> <p>The final objective is to create a new applicant object and load the CreaterPartner page loaded with the new tab either using page requests or through jQuery Ajax.</p> <p>To give more clarify, here is the ApplicantInformation.ascx code</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;DA.Net.Models.BaseModels.Applicant&gt;" %&gt; </code></pre> <p>And here is a part of PersonalInformation.ascx</p> <pre><code> &lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;DA.Net.Models.BaseModels.Applicant&gt;" %&gt; &lt;%@ Import Namespace="DA.Net.Models.BaseModels" %&gt; &lt;h3&gt;Personal Information&lt;/h3&gt; &lt;div&gt; &lt;table width="100%"&gt; &lt;tr&gt; &lt;td&gt;&lt;%= Html.LabelFor(model =&gt; model.FName) %&gt;&lt;/td&gt; &lt;td&gt; &lt;%= Html.TextBoxFor(model =&gt; model.FName).ToString().Replace("FName", "ApplicantsList[" + Model.ApplicantCounterID + "].FName")%&gt; &lt;%= Html.ValidationMessage("ApplicantsList[" + Model.ApplicantCounterID + "].FName")%&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Any guidance on this topic would be appreciated. I am not sure if I am missing something or if its not possible.</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