ASP.Net MVC 3: Baby for Children and Redirection

I need to display the registration form and the registration form on the main page.

If validation validation fails in these two forms, I need to display the correct errors on the main page.

But if there were no errors, the user should be redirected to a secure dashboard.

To accomplish this, I use child action on the home page as follows:

 @Html.Action("Register", "Membership") 

It works fine, as expected, if there are any errors, as it is capable of re-render partial view with the correct model , which has validation state information.

But if there were no errors when he tries to redirect, he gives an error message:

 Child actions are not allowed to perform redirect actions. 

Is there any way around this? I am sure that there is a way to place registration and registration forms on the main page. Most likely, I do not know, since I am new to ASP.Net MVC.

Could you point me in the right direction here?

+6
source share
3 answers

One way to do this is to use ajax forms for registration and registration bits, and instead of returning RedirectResult when the feed is valid, return some json that the client side of the script will track a bit and use a redirect for you.

Here is a simplified example.

Controller:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel.DataAnnotations; using MvcApplication12.Models; namespace MvcApplication12.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Register() { return PartialView(new UserDetails()); } [HttpPost] public ActionResult Register(UserDetails details) { if (ModelState.IsValid) { return Json(new {redirect = true, url = Url.Action("Index","Dashboard")}); } else { return PartialView(details); } } } } 

Index Home:

 @{ ViewBag.Title = "Index"; } <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script type="text/javascript"> function checkForRedirect(data) { if (data.redirect && data.url) { location.href = data.url; } } </script> <p>Home page stuff.....</p> <div id="RegistrationArea"> @Html.Action("Register") </div> <p> Home page stuff.....</p> 

Registration form "Register" partial view:

 @model MvcApplication12.Models.UserDetails <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "POST", Url = Url.Action("Register", "Home"), OnSuccess = "checkForRedirect(data)", UpdateTargetId = "RegistrationArea", InsertionMode = InsertionMode.Replace })) { @Html.LabelFor(m => m.UserName) @Html.EditorFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName) @Html.LabelFor(m => m.Password) @Html.EditorFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password) <input type="submit" /> } 
+6
source

You should not use child actions in this context.

The solution to your problem may be to place two forms on your page, one for registration and one for login. The registration form is sent to the "Register" action in the membership controller, in action to enter the "Login" action in the membership controller.

If an error occurs in one of the actions, you can:

  • Show custom login / registration page and use model / validation results to display error messages
  • Redirect to the URL / action the user came in with and show the error message that you post to TempData li>
+2
source

Without using javascript to redirect: If you put forms in your child’s views, sometimes if you specify the action name and controller name in the Beginform helper (inside the child view), this problem does not occur. for example, I changed the look of my child action as follows:

Before:

 @using (Html.BeginForm()) { ... } 

After:

  @using (Html.BeginForm("InsertComment", "Comments", FormMethod.Post, new { id = "commentform" })) { ... } 

Now you can put the RedirectAction command inside the "InsertComment" action, and everything will work.


Two forms in one page control:

1. Specify a name for the "Submit" button (each form) (example: "submitvalue")

form1:

  <input type="submit" value="login" name="submitValue" class="btn btn-success pull-right" /> 

form2:

  <input type="submit" value="register" name="submitValue" class="btn btn-success pull-right" /> 

2. Do two actions for these forms. (For example: "Registration" and "Login")

 [HttpPost] public ActionResult Login(LoginVM model, string submitValue) { if (submitValue == "login") { //Do Something } ... } [HttpPost] public ActionResult Register(RegisterVM model, string submitValue) { if (submitValue == "register") { //Do Something } ... } 
  • If you click the register or enter the form button, both actions are called up, but with the expression "if" we determine which one is our goal.
+1
source

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


All Articles