0

Still trying to figure that out, here is my workflow:

  1. once submit been clicked, the jquery will send a post request to call the method
  2. method return a partial view
  3. display on <div id = "messageForm">...</div> part

below is the form view:

//SignUp.cshtml:
<div id ="messageForm">

@using (Ajax.BeginForm("SignUp", "MVP", new AjaxOptions
{

    Confirm = "Are you sure you want to send this message?",
    HttpMethod = "Post",                                 
    InsertionMode = InsertionMode.Replace,                                 
    LoadingElementId = "loading",                                 
    UpdateTargetId = "messageForm"
})) {

      @Html.AntiForgeryToken();
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>
            messageModel

        </legend>

         <p>
            <input type ="submit" value ="Send Message" />
        </p>

    </fieldset>

here is the controller:

//MVPController
       [HttpPost]
            public ActionResult SignUp(MVCView model){
                return PartialView("_ThankYou");

            }

public ActionResult SignUp(){
                return View();
            }

Here is the partial view in view folder:

ThankYou.cshtml:

    <h1>Thank you so much! We will contact you later</h1>

When testing it, I didn't see the confirm dialog and it redirect to the thank you page

Can anyone tell me why that happened?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
LifeScript
  • 1,116
  • 5
  • 15
  • 24
  • Controller method is called SignUp, while the action in Html.AjaxForm is "ThankYou", are you sure it works? – Zruty Sep 04 '13 at 19:57
  • I have added an answer assuming that you have an error? there's nothing that states this in the question? – Christian Phillips Sep 04 '13 at 20:01
  • Do you have anything funny going on in your markup (like nested forms) because it sounds like the form isn't being submitted asynchronously? – Ant P Sep 04 '13 at 20:04
  • Have you referenced the `jquery.unobtrusive-ajax.js` file in your layout/view? Because this file is required in order to the `Ajax.BeginForm` work... – nemesv Sep 04 '13 at 20:07
  • close your #messageForm DIV prior to start a form. If you had it contain the form - thats a bad idea either as results will keep coming into self included DIVs. – Max Malyk Sep 04 '13 at 20:09
  • @nemesv in my web config there is:  – LifeScript Sep 04 '13 at 20:13
  • @Max Malyk I tried, but it still refresh the page and show the partial view again. =[ – LifeScript Sep 04 '13 at 20:15
  • 1
    @LifeScript you need to have a script include for `` in your layout or in your view. This file is not included by the default mvc project templates you need to add it manually and it has nothing to do with your web.config. – nemesv Sep 04 '13 at 20:15
  • @nemesv Perfect point! It works because of you! would you like to make a good answer for that, so I can make it correct solution? – LifeScript Sep 04 '13 at 20:27
  • possible duplicate of [ASP.NET MVC 4 AJAX Submit Form not working](http://stackoverflow.com/questions/9066244/asp-net-mvc-4-ajax-submit-form-not-working) – nemesv Sep 04 '13 at 20:31
  • @LifeScript, so there were two issues? wrong action name and no JS file? – Christian Phillips Sep 04 '13 at 20:43
  • @christiandev yes, I modified the action name and imported the js file, it works fine now! – LifeScript Sep 04 '13 at 20:49
  • posible duplicate http://stackoverflow.com/a/15662750/138071 – andres descalzo Sep 04 '13 at 21:35

1 Answers1

1

You have:

@using (Ajax.BeginForm("ThankYou", "MVP", new AjaxOptions

I think this should be:

@using (Ajax.BeginForm("SignUp", "MVP", new AjaxOptions

The first string is the Action name, and you only have SignUp in the controller.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82