Go---Go back to the original interface after logging in ASP.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.