What is a good way to pass a confirmation or error

2019-05-21 02:49发布

When the user clicks the submit button on a form I want to return a success / failure message to the user and i'm wondering what the best way to send the message back to the user is.

For example on a login form if the user enters an incorrect password i'd want the view to reload with a message telling them that login failed. Or on an update form the original view they had would show up with a message saying update was successful.

My idea is to have a few partial views or HTML helpers that look for a ViewBag property like "ErrorMessage" or "SuccessMessage" and conditionally show themselves when these properties have a value. These components could be added to the _Layout or manually each form where they are required.

Edit

I have since found out that the ValidationSummary method takes a parameter of whether to exclude property errors which means you can use it to show when login / registration has failed. But there seems to be a bug where the validation summary is still generated even if there are no errors.

2条回答
▲ chillily
2楼-- · 2019-05-21 03:04

This is a good question and one that I've been trying to solve in a new MVC application recently. For me, ModelState.AddModelError is ok for errors but doesn't work when communicating things like success or warning messages.

Here's an approach that I decided to take recently and I welcome any feedback on it:

  1. I created a class called Notification and an enum called NotificationType. NotificationType has 4 values: Success, Error, Warning, & Info. Notification has the properties 'Message' and 'Type'.

  2. I have a static class called NotificationManager that holds instances of Notification objects for the life of an HttpRequest (using the user's Session object). The idea is that any controller can add any number of Notification objects during the execution of an Action depending on what it needs to "tell" the user.

  3. I have a custom HtmlHelper which I call once in a master page (i.e. Html.NotificationSummaryFor()) that takes a IEnumerable of Notifications (i.e. NotificationManager.Notifications) and renders a UL with child LI's for every Notification. I then use CSS classes to style those notification LI's based on the 'NotificationType' (Red for Error, Yellow for Warning, Green for Success, etc). The HtmlHelper is also responsible for clearing the notifications from the manager after rendering them to the View.

This implementation is pretty simple and has limitations but in general, I find that having a framework like this ensures that a team of developers working on the same application are able to provide notifications in a consistent fashion which is important to usability and maintainability.

查看更多
神经病院院长
3楼-- · 2019-05-21 03:15

asp-net-mvc-2-model-validation This is a really easy way to set up Validation. Also if you are doing things like login you can do something like this where you add a modelError to the model state and then check for that in your code

 if (ModelState.IsValid) {
            if (!FormsAuthentication.Authenticate(userName , password))
                ModelState.AddModelError("" , "Incorrect username or password");
        }

        if (ModelState.IsValid) {
            FormsAuthentication.SetAuthCookie(userName  , false);
            return Redirect(url ?? Url.Action("Index" , "Admin"));
        } else
            return View(); //goes right back to the log on screen 
查看更多
登录 后发表回答