Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use partial views. I use the following approach:</p> <p>Use a view model so you're not passing your domain models to your views:</p> <pre><code>public class EditPersonViewModel { public int Id { get; set; } // this is only used to retrieve record from Db public string Name { get; set; } public string Age { get; set; } } </code></pre> <p>In your <code>PersonController:</code></p> <pre><code>[HttpGet] // this action result returns the partial containing the modal public ActionResult EditPerson(int id) { var viewModel = new EditPersonViewModel(); viewModel.Id = id; return PartialView("_EditPersonPartial", viewModel); } [HttpPost] // this action takes the viewModel from the modal public ActionResult EditPerson(EditPersonViewModel viewModel) { if (ModelState.IsValid) { var toUpdate = personRepo.Find(viewModel.Id); toUpdate.Name = viewModel.Name; toUpdate.Age = viewModel.Age; personRepo.InsertOrUpdate(toUpdate); personRepo.Save(); return View("Index"); } } </code></pre> <p>Next create a partial view called <code>_EditPersonPartial</code>. This contains the modal header, body and footer. It also contains the Ajax form. It's strongly typed and takes in our view model.</p> <pre><code>@model Namespace.ViewModels.EditPersonViewModel &lt;div class="modal-header"&gt; &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;×&lt;/button&gt; &lt;h3 id="myModalLabel"&gt;Edit group member&lt;/h3&gt; &lt;/div&gt; &lt;div&gt; @using (Ajax.BeginForm("EditPerson", "Person", FormMethod.Post, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "POST", UpdateTargetId = "list-of-people" })) { @Html.ValidationSummary() @Html.AntiForgeryToken() &lt;div class="modal-body"&gt; @Html.Bootstrap().ControlGroup().TextBoxFor(x =&gt; x.Name) @Html.Bootstrap().ControlGroup().TextBoxFor(x =&gt; x.Age) &lt;/div&gt; &lt;div class="modal-footer"&gt; &lt;button class="btn btn-inverse" type="submit"&gt;Save&lt;/button&gt; &lt;/div&gt; } </code></pre> <p></p> <p>Now somewhere in your application, say another partial _peoplePartial.cshtml etc:</p> <pre><code>&lt;div&gt; @foreach(var person in Model.People) { &lt;button class="btn btn-primary edit-person" data-id="@person.PersonId"&gt;Edit&lt;/button&gt; } &lt;/div&gt; // this is the modal definition &lt;div class="modal hide fade in" id="edit-person"&gt; &lt;div id="edit-person-container"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $('.edit-person').click(function () { var url = "/Person/EditPerson"; // the url to the controller var id = $(this).attr('data-id'); // the id that's given to each button in the list $.get(url + '/' + id, function (data) { $('#edit-person-container').html(data); $('#edit-person').modal('show'); }); }); }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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