質問

I really like how the ModelState dictionary works when submitting forms (how you can add errors to it if they happen and then the errors are displayed when the form reloads).

However, what about for non-error messages? For instance, if I wanted to have the same form load after submission, is there a standard way of displaying a message that the form was submitted successfully?

役に立ちましたか?

解決

Take a look at TempData. It's a key/value store that is good for the subsequent request and then it is destroyed.

Here is a good article on when to use it vs ViewData and ViewBag.

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

However, you should be returning strongly typed ViewModels to your view, and therefore you could have a base ViewModel that could have a message dictionary on it:

public class ViewModelBase
{
    public Dictionary<string,string> Messages { get; set; }
}

And with a little extension method you could have a very easy API to display in the view:

@Model.Messages.DisplayAll()

You would have to implement DisplayAll(), but you get the gist.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top