开发者

MVC Ajax.BeginForm [closed]

开发者 https://www.devze.com 2023-01-22 07:00 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

I have an ajax form like so:

    <% using (Ajax.BeginForm("Comments", "Comments", null, new AjaxOptions {UpdateTargetId="main", OnSuccess="createSuccess" }, new{ @id="main"})) {%>
    <%: Html.ValidationSummary(true, "errors") %>

    <fieldset>
        <legend>Fields</legend>

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

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

        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>

<% } %>

<div id="Success" style="display:none;">
    New entry successfully entered
</div>

<script type="text/javascript">
function createSuccess(context)
{
    if ($(".validation-summary-errors").length > 0) {
        return;
    }

    $('#main').clearForm()
    $('#Success').show();
}

Is this the right way to check if the form was successfully submitted? What if the class开发者_如何学运维 name for validation-summary changes? is there a better way?

thanks


You are definitely on the right track. The important thing to know, is that in order for the server side validation messages to populate, you need the Comments action to return a partial which contains the form you have above, because when the ajax call returns it will replace whatever has id="main" (your form), with the result of the action.

0

精彩评论

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

关注公众号