开发者

MVC 2 Ajax.Beginform passes returned Html + Json to javascript function

开发者 https://www.devze.com 2022-12-24 21:53 出处:网络
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.

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.

 <% using (Ajax.BeginForm("Create", new AjaxOptions { OnComplete = "ProcessResponse" }))
          {%>
        <f开发者_C百科ieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%=Html.LabelFor(model => model.FirstName)%>
            </div>
            <div class="editor-field">
                <%=Html.TextBoxFor(model => model.FirstName)%>
                <%=Html.ValidationMessageFor(model => model.FirstName)%>
            </div>

            <div class="editor-label">
                <%=Html.LabelFor(model => model.LastName)%>
            </div>
            <div class="editor-field">
                <%=Html.TextBoxFor(model => model.LastName)%>
                <%=Html.ValidationMessageFor(model => model.LastName)%>
            </div>

            <p>
             <input type="submit" />

            </p>
        </fieldset>
<%
           }
%>

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.

$('#personTable tr:last').after(data);

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.

I have tried returning a Json array

Controller:

 return Json(new
                {
                    Success = true,
                    Html= this.RenderViewToString("PersonSubform",person)

                });

Javascript:

var json_data = response.get_response().get_object();

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.

SO RenderPartialToString

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?

UPDATE

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.

function ProcessResponse(response) {

            var html = response.get_data();

            $("#campaignSubform").html(html);

        }

Many thanks in advance


what I've done in this circumstance is actually render out a partial view that has all the information (e.g. validation info, etc) and then use DOM functions in jquery to extract what I need, if it's more than just for display. Keeps everything in one format that way and I don't have to wory about handling both json and html.


Why do you even need to pass back the partial person object? As far as I'm aware ASP.NET MVC should leave the form filled out as the user filled it in but add validation errors to the form.

They can then correct the errors and submit the form again. Sound more like a problem with the design of the web site and something that you shouldn't be doing in the first place.

If you need to store the data the user entered I would create a json person object and submit this object with the ajax request and then when the validation errors come back you already have the data that was sent.

Ajax enables you to have state in the web client as long as the user is viewing the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号