Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use AJAX. Start by wrapping the webGrid into an html form which will allow for editing the record:</p> <pre><code>@using (Html.BeginForm("Update", null, FormMethod.Post, new { @class = "updateForm" })) { @grid.GetHtml( headerStyle: "headerStyle", tableStyle: "tableStyle", alternatingRowStyle: "alternateStyle", fillEmptyRows: true, mode: WebGridPagerModes.All, firstText: "&lt;&lt; First", previousText: "&lt; Prev", nextText: "Next &gt;", lastText: "Last &gt;&gt;", columns: new[] { grid.Column("ID",header: "ID"), grid.Column("LoginId",header:"LoginID"), grid.Column("FirstName", canSort: false), grid.Column("LastName"), grid.Column("CreatedOn", header: "CreatedOn", format: p=&gt;p.CreatedOn.ToShortDateString() ), grid.Column("", header: "Actions", format: @&lt;text&gt; @Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { @class = "edit" }) | @Html.ActionLink("Delete", "Delete", new { id = item.ID }) &lt;/text&gt; ) } ) } </code></pre> <p>Then you could have 2 partials, one for editing and one for displaying a single record.</p> <p><code>EditUserDetailsModel.cshtml</code>:</p> <pre><code>@model UserDetailsModel &lt;td&gt;@Html.EditorFor(x =&gt; x.ID)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(x =&gt; x.LoginID)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(x =&gt; x.FirstName)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(x =&gt; x.LastName)&lt;/td&gt; &lt;td&gt;@Html.EditorFor(x =&gt; x.CreatedOn)&lt;/td&gt; &lt;td&gt; &lt;button type="submit"&gt;Update&lt;/button&gt; &lt;/td&gt; </code></pre> <p><code>DisplayUserDetailsModel</code>:</p> <pre><code>@model UserDetailsModel &lt;td&gt;@Html.DisplayFor(x =&gt; x.ID)&lt;/td&gt; &lt;td&gt;@Html.DisplayFor(x =&gt; x.LoginID)&lt;/td&gt; &lt;td&gt;@Html.DisplayFor(x =&gt; x.FirstName)&lt;/td&gt; &lt;td&gt;@Html.DisplayFor(x =&gt; x.LastName)&lt;/td&gt; &lt;td&gt;@Html.DisplayFor(x =&gt; x.CreatedOn)&lt;/td&gt; &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id = Model.ID }, new { @class = "edit" }) | @Html.ActionLink("Delete", "Delete", new { id = Model.ID }) &lt;/td&gt; </code></pre> <p>and then we could have the corresponding controller actions:</p> <pre><code>public ActionResult Edit(int id) { UserDetailsModel model = repository.Get(id); return PartialView("EditUserDetailsModel", model); } [HttpPost] public ActionResult Update(UserDetailsModel model) { repository.Update(model); return PartialView("DisplayUserDetailsModel", model); } </code></pre> <p>and finally the javascript to make this alive:</p> <pre><code>$('table').on('click', '.edit', function () { $.ajax({ url: this.href, type: 'GET', cache: false, context: $(this).closest('tr'), success: function (result) { $(this).html(result); } }); return false; }); $('.updateForm').on('submit', function () { $.ajax({ url: this.action, type: this.method, data: $(this).serialize(), context: $('input', this).closest('tr'), success: function (result) { $(this).html(result); } }); return false; }); </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. 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