ASP.Net MVC partial views keeping their model state?

前端 未结 5 1450
小蘑菇
小蘑菇 2021-02-08 19:43

This is probably again a newbie question.

When I create an ASP.NET MVC2 application, an Account Controller with an Action LogIn is created like this:

[H         


        
5条回答
  •  滥情空心
    2021-02-08 19:56

    Yeah redirecttoaction but provide an error message with it in the tempdata so you should do something like this

    TempData["errorMsg"] = "incorrect values provided";
    return RedirectToAction("Index", "Home")
    

    Of course in the main index you should have a div that displays the message

    <%= html.Encode(TempData["errorMsg"]) %>
    

    EDIT I see you want to maintain the modelstate that might be a problem but what you could do is pass the modelstate in the index action or pass the modelstate object in the tempdata. What you can do then is check if a there are modelstate errors in the object and if there are check the field and add the error to the right field.

提交回复
热议问题