Partial ASP.NET MVC View submit

I'm new to ASP.NET MVC, so the question may seem "dumb", sorry.

I have a partial view inside my home view.

A partial view represents a form that invokes an action method inside a HomeController.

It works fine with server verification, the problem is that after publishing only Partial View is displayed.

How can I display the whole view of a house after posting?

About the code:

Inside PartialView, I have a form:

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

A request is an 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 ascx message, the server authentication files are echoed, but only the ascx PartialView code is rendered. Url looks like this after posting:

http://xxxxxxxxxxx/Home/Request

I want to show the whole home view with ascx inside, showing server check errors.

+3
3

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>

, /ascx/HTML :

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

:

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

I ran into the same problem in the code, so I just made a little modification in my code and it worked. Instead of returning the same point of view, I used

return Redirect(Request.Referrer)

Earlier:

return View();
+2
source

Source: https://habr.com/ru/post/1772079/


All Articles