Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Further to my comment against @JBall's answer that helped me along the way, this is the final answer that works for me. I'm using MVC and Razor and I'm submitting a form using jQuery AJAX so I can update a partial view with some new results and I didn't want to do a complete postback (and page flicker).</p> <p>Add the <code>@Html.AntiForgeryToken()</code> inside the form as usual.</p> <p>My AJAX submission button code (i.e. an onclick event) is:</p> <pre><code>//User clicks the SUBMIT button $("#btnSubmit").click(function (event) { //prevent this button submitting the form as we will do that via AJAX event.preventDefault(); //Validate the form first if (!$('#searchForm').validate().form()) { alert("Please correct the errors"); return false; } //Get the entire form's data - including the antiforgerytoken var allFormData = $("#searchForm").serialize(); // The actual POST can now take place with a validated form $.ajax({ type: "POST", async: false, url: "/Home/SearchAjax", data: allFormData, dataType: "html", success: function (data) { $('#gridView').html(data); $('#TestGrid').jqGrid('setGridParam', { url: '@Url.Action("GetDetails", "Home", Model)', datatype: "json", page: 1 }).trigger('reloadGrid'); } }); </code></pre> <p>I've left the "success" action in as it shows how the partial view is being updated that contains an MvcJqGrid and how it's being refreshed (very powerful jqGrid grid and this is a brilliant MVC wrapper for it).</p> <p>My controller method looks like this:</p> <pre><code> //Ajax SUBMIT method [ValidateAntiForgeryToken] public ActionResult SearchAjax(EstateOutlet_D model) { return View("_Grid", model); } </code></pre> <p>I have to admit to not being a fan of POSTing an entire form's data as a Model but if you need to do it then this is one way that works. MVC just makes the data binding too easy so rather than subitting 16 individual values (or a weakly-typed FormCollection) this is OK, I guess. If you know better please let me know as I want to produce robust MVC C# code.</p>
    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.
    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