Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 2 Ajax.Beginform passes returned Html + Json to javascript function
    primarykey
    data
    text
    <p>I have a small partial Create Person form in a page above a table of results. I want to be able to post the form to the server, which I can do no problem with ajax.Beginform. </p> <pre><code> &lt;% using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "ProcessResponse" })) {%&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;div class="editor-label"&gt; &lt;%=Html.LabelFor(model =&gt; model.FirstName)%&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%=Html.TextBoxFor(model =&gt; model.FirstName)%&gt; &lt;%=Html.ValidationMessageFor(model =&gt; model.FirstName)%&gt; &lt;/div&gt; &lt;div class="editor-label"&gt; &lt;%=Html.LabelFor(model =&gt; model.LastName)%&gt; &lt;/div&gt; &lt;div class="editor-field"&gt; &lt;%=Html.TextBoxFor(model =&gt; model.LastName)%&gt; &lt;%=Html.ValidationMessageFor(model =&gt; model.LastName)%&gt; &lt;/div&gt; &lt;p&gt; &lt;input type="submit" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; </code></pre> <p>Then in my controller I want to be able to post back a partial which is just a table row if the create is successful and append it to the table, which I can do easily with jquery.</p> <pre><code>$('#personTable tr:last').after(data); </code></pre> <p>However, if server validation fails I want to pass back my partial create person form with the validation errors and replace the existing Create Person form.</p> <p>I have tried returning a Json array </p> <p>Controller:</p> <pre><code> return Json(new { Success = true, Html= this.RenderViewToString("PersonSubform",person) }); </code></pre> <p>Javascript: </p> <pre><code>var json_data = response.get_response().get_object(); </code></pre> <p>with a pass/fail flag and the partial rendered as a string using the solition below but that doesnt render the mvc validation controls when the form fails.</p> <p><a href="https://stackoverflow.com/questions/483091/render-a-view-as-a-string/1241257#1241257">SO RenderPartialToString</a></p> <p>So, is there any way I can hand my javascript the out of the box PartialView("PersonForm") as its returned from my ajax.form? Can I pass some addition info as a Json array so I can tell if its pass or fail and maybe add a message?</p> <p>UPDATE</p> <p>I can now pass the HTML of a PartialView to my javascript but I need to pass some additional data pairs like ServerValidation : true/false and ActionMessage : "you have just created a Person Bill". Ideally I would pass a Json array rather than hidden fields in my partial.</p> <pre><code>function ProcessResponse(response) { var html = response.get_data(); $("#campaignSubform").html(html); } </code></pre> <p>Many thanks in advance</p>
    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.
 

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