3

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.

2
  • 1
    use the asp.net-mvc label as 'mvc' is a general label. Commented Oct 29, 2010 at 9:37
  • your tag is fine. you just have to edit your question and add some details and a little source code Commented Oct 29, 2010 at 9:40

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

Comments

2

Change return type also:

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

Comments

2

I was facing same issue in code, so I just made a small modification in my code and it worked. Instead of returning the same view, I used

return Redirect(Request.Referrer)

Earlier:

return View();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.