Asp.net-mvc – Partial ASP.NET MVC View submit

asp.netasp.net-mvcasp.net-mvc-partialview

I'm new in ASP.NET MVC so the question could appear 'stupid', sorry.

I have a Partial View inside my Home view.

The Partial View submit a form calling an Action Method inside the HomeController.

It works fine with server validation, the problem is that after the post only the Partial View is rendered.

How can I render the entire Home view after post?

About the code:

Inside PartialView I have a form:

<% using (Html.BeginForm("Request", "Home")) { %>

Request is a ActionResult defined inside my HomeController.

[HttpPost]
public ActionResult Request(RequestModel model)
{
  if (ModelState.IsValid)
  {
    // Saving data .....
  }
  else
  {
     // Show Server Validation Errors
     return View();
  }
}

At this time, after the post, the ascx shows the server validation erros but only the PartialView ascx code is rendered.
The Url looks like this after the post:

http://xxxxxxxxxxx/Home/Request

What I want is showing the entire Home view with the ascx inside showing server validation errors.

Best Answer

Try to do a partial submit using jQuery:

<script type="text/javascript">
$(document).ready(function () {
    $("input[type=submit]").live("click", function () {
        var f = $("input[type=submit]").parents("form");
        var action = f.attr("action");
        var serializedForm = f.serialize();
        $.ajax({
            type: 'POST',
            url: action,
            data: serializedForm,
            success: function (data, textStatus, request) {
                if (!data == "") {
                    // redisplay partial view
                    $("#formDiv").html(data);
                }
                else {
                    // do whatever on sucess
                }
            }
        });
        return false;
    });
});
</script>

Assuming your view/ascx/HTML is something like this:

<div id="formDiv">
    <% Html.RenderAction("Request"); %>
</div>