Log on to ASP. net mvc and return to the original interface. asp. netmvc
To submit a form, go to the logon page if the user does not log on. After logging on, go to the page where the original form is submitted, and maintain the data on the submit form interface.
The page for submitting a form is a strongly typed view page. If you do not need to maintain the data on the submit form interface, you can first design a Model like this:
public class Student
{ public string Name{get;set;} public string ReturnUrl{get;set;}}
On the view page of the submitted form, write roughly as follows:
@using (Html.BeginForm("Index", "Home", FormMethod.Post)){ @Html.Hidden("ReturnUrl", Request.Url.PathAndQuery) @Html.TextBoxFor(m => m.Name)
<Input type = "submit" value = "submit"/>
}
In the controller, It is roughly written as follows:
public ActionResult Index()
{ return View(new Student());
}
[HttpPost]
public ActionResult Index(Student student)
{ return Redirect(student.ReturnUrl);
}
However, although the strong type view page for form submission is returned, the form data is not maintained.
So I thought of the following method:
Return View ("someview", somemodel );
How can I obtain the someview name?
public ActionResult Index()
{ return View(new Student());
}
Above, if we get the action name, it is equivalent to getting the view name!
Redesign Model:
public class Student
{ public string Name { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } }
You can obtain the action name from the route and assign it to the ActionName attribute of Student.
public class HomeController : Controller
{
public ActionResult Index()
{ Student student = new Student()
{ ActionName = this.ControllerContext.RouteData.Values["action"].ToString(),
ControllerName = this.ControllerContext.RouteData.Values["controller"].ToString()
};
return View(student);
}
[HttpPost]
public ActionResult Index(Student student)
{ViewBag. msg = "I'm back ~~ ";
// If you are logged on, first verify and verify that the following code is successfully executed
return View(student.ActionName, student);
}
}
Above, the value of student. ActionName is both the action name and view name.
On the strongly typed view page of the submitted Form:
@model MvcApplication1.Models.Student
@{ ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>@ViewBag.msg</div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post)){ @Html.TextBoxFor(m => m.Name)
<Input type = "submit" value = "submit"/>
}
Therefore, in the face of the requirements described in this article, only jump is not enough. You need to pass the Model to a view, and the key is:
1. Get the action name from the route
2. The action name is the same as the view name.