ASP.NET MVC - How to maintain ModelState from a different controller?

后端 未结 5 2405
时光取名叫无心
时光取名叫无心 2021-02-20 18:43

I have a HomeController with an Index action that shows the Index.aspx view. It has a username/password login section. When the user clicks the submit button, it POSTs to a Lo

5条回答
  •  深忆病人
    2021-02-20 19:18

    As others have said it's common to return the view if validation fails but as you are calling from your account controller you will want to specify the full path of your view

    return View("~/Views/Home/Index.aspx", model);
    

    Or

    It is also common to have a seperate login page and redirect to that page if the login fails. Both pages will submit to the same login action. Facebook does this for example.

    Or

    As you only want to display an error message

    return RedirectToAction("Index", "Home", new { LoginAttempts = 1 });
    

    then in your Index action read the LoginAttempts parameter and choose to display the error message accordingly.

提交回复
热议问题