Home > DeveloperSection > Forums > JQuery Dialog box not closed in ASP.NET MVC 4
Takeshi Okada
Takeshi Okada

Total Post:89

Points:629
Posted on    January-30-2013 12:54 AM

 ASP.NET MVC ASP.NET MVC 
Ratings:


 1 Reply(s)
 2983  View(s)
Rate this:

Hi Everyone!

I am using a dialog box with jQuery in MVC 4, my code as below

    <div id="dialogBox" title="Select Products" style="overflow: hidden;">
    </div>


    $('#dialogBox').load("@Url.Action("SelectProducts")",
                    function(response, status, xhr) {
                        $('#dialogBox').dialog('open');
    });

When I clicks on submit button in the dialog box I got to httppost as such


     [HttpPost]
     public ActionResult SelectProducts(Products model)
     {
        if (ModelState.IsValid)
        {
            lbService.ProductsData(model);
            return View(model); // success
        }

        return View(model); // problem          

     }

 

Thanks in advance



AVADHESH PATEL

Total Post:604

Points:4228
Posted on    January-30-2013 6:03 AM

Hi Takeshi Okada!

Used an Ajax.BeginForm inside the partial that you are loading and put the contents that you want to be loaded in the dialog inside a partial and also have your HttpPost controller action return this same partial view instead of returning an entire view.

Include the jquery.unobtrusive-ajax.js script to your page for Ajax.BeginForm form to work.
As an alternative to using Ajax.BeginForm you could still use a normal Html.BeginForm and manually AJAXify it:
$(document).on('submit', '#dialogBox form', function() {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function(result) {
            if (result.success) {
                $('#dialogBox').dialog('close');
            } else {
                $('#dialogBox').html(result);
            }
        } 
    });
    return false;
});


Don't want to miss updates? Please click the below button!

Follow MindStick