There is a requirement: submit the form, if the user is not logged in, jump to the login page, log in, jump to the original form to submit this page, and need to keep the data submitted form interface.
The page that submits the form is a strongly typed view page, and if you do not consider the data that you need to keep submitting the form interface, you can design a model like this:
public class Student
{
public string Name{get;set;}
public string Returnurl{get;set;}
}
On the view page that submits the form, this is roughly the case:
@using (Html.BeginForm ("Index", "Home", FormMethod.Post))
{
@Html. Hidden ("ReturnUrl", Request.Url.PathAndQuery)
@Html. textboxfor (M = m.name)
<input type= "Submit" value= "Submission"/>
}
In the controller, write roughly this:
Public ActionResult Index ()
{
Return View (New Student ());
}
[HttpPost]
Public ActionResult Index (Student Student)
{
Return Redirect (student. RETURNURL);
}
However, while returning to the strongly typed view page of the form submission, the form data was not persisted.
So, think of using the following methods:
Return View ("Someview", Somemodel);
How does the Someview name get it?
Public ActionResult Index ()
{
Return View (New Student ());
}
Above, if we get the name of the action is equivalent to get the name of the view!
Redesign Model:
public class Student
{
public string Name {get; set;}
public string Controllername {get; set;}
public string ActionName {get; set;}
}
You can get the action name from the route and assign it to the ActionName property 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 am back again ~ ~";
If it is logged in, verify first, verify that the following code is executed successfully
Return View (student. ActionName, student);
}
}
Above, student. The ActionName value is both the action name and the view name.
On the strongly typed view page that submits the 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= "Submission"/>
}
Therefore, in the face of the requirements described in this article, just jump is not enough, you need to pass a model to a view, the key is:
1. Get the action name from the route
2. Action name and view name match
Go---Go back to the original interface after logging in ASP.