Question

In my LoginController I have the following action method:

[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl) {
    if (ModelState.IsValid) {
       if (authProvider.Authenticate(model.UserName, model.Password)) {
          return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
       } else {
          ModelState.AddModelError("", "Incorrect username or password");
          return View();
         }
    } else {
       return View();
    }
}

And the corresponding view is strongly typed with LoginViewModel (also the first argument to the above action method) class and this is how the action method is called:

  @using(Html.BeginForm()) {
    @Html.EditorForModel()
   <p><input type="submit" value="Log in" /></p>
 }

Now I have 2 questions:

1.There is another action method with the same name in the controller. The difference is the other one doesn't have [HttpPost] attribute. Why is the above action method called but not the other one?

2.Both arguments of the action method are different from null. What information is passed to the invoked action method. Can it be inferred that model and the url is passed to the method? If so, then in what circumstances are these two prameters passed and when they are not?

Was it helpful?

Solution

Here are the answers:

1.Default HTTP Method for Html.BeginForm() is POST,so when you submit form,Action method with [HttpPost] is invoked, where as other Action method with same name is invoked for HTTP GET method.

2.When you Log In i.e by clicking "Log in" button LoginViewModel will be passed and returnUrl will be passed when you're redirected to Log in page when you've tried to access directly another page without logging where returnUrl set to initial page you've requested.

See below link for BeginForm details.

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.formextensions.beginform%28v=vs.118%29.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top