Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

jquery - MVC 3 Razor - Ajax.BeginForm OnSuccess

I'm new to MVC and I'm trying to update my page after I submit my form; but it's not working. I'm only trying to hide the form, and show the contents of a div OnSuccess.

My code is:

<script type="text/javascript">
    $(document).ready(function () {
        $('#confirmation').hide();
    });

    function MessageConfirmation() {
        $('#confirmation').show('slow');
        $('#contactForm').hide('slow');
    }

</script>

@using (Ajax.BeginForm("Index", new AjaxOptions { OnSuccess = "MessageConfirmation" }))
{
<fieldset id="contactForm">
    <legend>Message</legend>
    <p>
        @Html.Label("Email", "Email"): @Html.TextBox("Email")
    </p>
    <p>
        @Html.Label("Subject", "Subject"): @Html.TextBox("Subject")
    </p>
    <p>
        @Html.Label("Message", "Message"): @Html.TextArea("Message")
    </p>
    <p>
        <input type="submit" value="Send" />
    </p>
</fieldset>

<p id="confirmation" onclick="MessageConfirmation()">
    Thanks!!!
</p>
}

Any alternate solutions / ideas are most welcome.

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not working is a problem description that's more adapted to people that don't know/care about how computer works and not software developers. Software developers usually describe precisely the problem they are having. They post the exact error message/exception stack trace they are having.

This being said you are asking for alternative solutions, here's mine: don't use any MS Ajax.* helpers, use jquery directly and unobtrusively, like this

<script type="text/javascript">
    $(function () {
        $('#confirmation').hide();
        $('form').submit(function() {
            $('#confirmation').show('slow');
            $(this).hide('slow');
            return false;
        });
    });
</script>

@using (Html.BeginForm())
{
    <fieldset id="contactForm">
        <legend>Message</legend>
        <p>
            @Html.Label("Email", "Email"): 
            @Html.TextBox("Email")
        </p>
        <p>
            @Html.Label("Subject", "Subject"): 
            @Html.TextBox("Subject")
        </p>
        <p>
            @Html.Label("Message", "Message"): 
            @Html.TextArea("Message")
        </p>
        <p>
            <input type="submit" value="Send" />
        </p>
    </fieldset>
}

<p id="confirmation">
    Thanks!!!
</p>

Notice how the confirmation paragraph has been externalized from the form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...