Log on to ASP. net mvc and return to the original interface. asp. netmvc

Source: Internet
Author: User

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.

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.