Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer to avoid using <code>Ajax.BeginForm</code> helper and do an Ajax call with JQuery. In my experience it is easier to maintain code written like this. So below are the details:</p> <p><strong>Models</strong></p> <pre><code>public class ManagePeopleModel { public List&lt;PersonModel&gt; People { get; set; } ... any other properties } public class PersonModel { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } ... any other properties } </code></pre> <p><strong>Parent View</strong></p> <p>This view contains the following things:</p> <ul> <li>records of people to iterate through</li> <li>an empty div that will be populated with a modal when a Person needs to be edited</li> <li>some JavaScript handling all ajax calls</li> </ul> <pre class="lang-html prettyprint-override"><code>@model ManagePeopleModel &lt;h1&gt;Manage People&lt;/h1&gt; @using(var table = Html.Bootstrap().Begin(new Table())) { foreach(var person in Model.People) { &lt;tr&gt; &lt;td&gt;@person.Id&lt;/td&gt; &lt;td&gt;@Person.Name&lt;/td&gt; &lt;td&gt;@person.Age&lt;/td&gt; &lt;td&gt;@html.Bootstrap().Button().Text("Edit Person").Data(new { @id = person.Id }).Class("btn-trigger-modal")&lt;/td&gt; &lt;/tr&gt; } } @using (var m = Html.Bootstrap().Begin(new Modal().Id("modal-person"))) { } @section Scripts { &lt;script type="text/javascript"&gt; // Handle "Edit Person" button click. // This will make an ajax call, get information for person, // put it all in the modal and display it $(document).on('click', '.btn-trigger-modal', function(){ var personId = $(this).data('id'); $.ajax({ url: '/[WhateverControllerName]/GetPersonInfo', type: 'GET', data: { id: personId }, success: function(data){ var m = $('#modal-person'); m.find('.modal-content').html(data); m.modal('show'); } }); }); // Handle submitting of new information for Person. // This will attempt to save new info // If save was successful, it will close the Modal and reload page to see updated info // Otherwise it will only reload contents of the Modal $(document).on('click', '#btn-person-submit', function() { var self = $(this); $.ajax({ url: '/[WhateverControllerName]/UpdatePersonInfo', type: 'POST', data: self.closest('form').serialize(), success: function(data) { if(data.success == true) { $('#modal-person').modal('hide'); location.reload(false) } else { $('#modal-person').html(data); } } }); }); &lt;/script&gt; } </code></pre> <p><strong>Partial View</strong></p> <p>This view contains a modal that will be populated with information about person.</p> <pre class="lang-cs prettyprint-override"><code>@model PersonModel @{ // get modal helper var modal = Html.Bootstrap().Misc().GetBuilderFor(new Modal()); } @modal.Header("Edit Person") @using (var f = Html.Bootstrap.Begin(new Form())) { using (modal.BeginBody()) { @Html.HiddenFor(x =&gt; x.Id) @f.ControlGroup().TextBoxFor(x =&gt; x.Name) @f.ControlGroup().TextBoxFor(x =&gt; x.Age) } using (modal.BeginFooter()) { // if needed, add here @Html.Bootstrap().ValidationSummary() @:@Html.Bootstrap().Button().Text("Save").Id("btn-person-submit") @Html.Bootstrap().Button().Text("Close").Data(new { dismiss = "modal" }) } } </code></pre> <p><strong>Controller Actions</strong></p> <pre class="lang-cs prettyprint-override"><code>public ActionResult GetPersonInfo(int id) { var model = db.GetPerson(id); // get your person however you need return PartialView("[Partial View Name]", model) } public ActionResult UpdatePersonInfo(PersonModel model) { if(ModelState.IsValid) { db.UpdatePerson(model); // update person however you need return Json(new { success = true }); } // else return PartialView("[Partial View Name]", model); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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