How do I provide success messages asp.net mvc?
How do I provide s开发者_运维技巧uccess messages in asp.net mvc?
If you're displaying a message on a different page than ViewData
won't help you, since it's reinitialized with each request. On the other hand, TempData
can store data for two requests. Here's an example:
public ActionResult SomeAction(SomeModel someModel)
{
if (ModelState.IsValid)
{
//do something
TempData["Success"] = "Success message text.";
return RedirectToAction("Index");
}
else
{
ViewData["Error"] = "Error message text.";
return View(someModel);
}
}
Inside if
block you must use TempData
because you're doing redirection (another request), but inside else you can use ViewData
.
And inside view you could have something like this:
@if (ViewData["Error"] != null)
{
<div class="red">
<p><strong>Error:</strong> @ViewData["Error"].ToString()</p>
</div>
}
@if (TempData["Success"] != null)
{
<div class="green">
<p><strong>Success:</strong> @TempData["Success"].ToString()</p>
</div>
}
in your controller, you can do this:
ViewData["Message"] = "Success"
and in your view you you can check if there is a message to display, and if so than display it:
@if (ViewData["Message"] != null)
<div>success</div>
Use ViewData to store success messages. Create the success message in the Controller and check for it in the View. If it exists, render it.
TempData can be used like a dictionary. Each saved value lasts for the current and the next request. Perfect for redirects.
this.TempData["messages"] = "Success!";
return RedirectToAction("YourAction");
Use ViewData["success"]
or ViewData["error"]
for instance:
public ActionResult saveResult()
{
var num = 1;
//After action was completed;
if ( num == 1 ) {
ViewData["success"] = "Action completed successfully";
}else{
ViewData["error"] = "OOps Action not completed";
}
return RedirectoAction("Index");
}
on the page you want to check if submission was successful
@if ( ViewData["success"] != null )
{
<div class="alert alert-success">@ViewData["success"]</div>
}
@if ( ViewData["error"] != null )
{
<div class="alert alert-error">@ViewData["error"]</div>
}
You can use the below generic approach so that it can be used to display all kind of messages like Success/Error/Info/Warning with a single implementation and can be used from any page.
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
</head>
<body>
@if (TempData["UserMessage"] != null)
{
var message = (Models.Message) TempData["UserMessage"];
<div class="alert @message.CssClassName alert-dismissible fade show">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>@message.Title</strong> @message.DisplayMessage
</div>
}
@RenderBody()
</body>
</html>
Controller
[HttpPost]
public ActionResult ActionMethod(ViewModel vm)
{
if(ModelState.IsValid)
{
try
{
CommitData();
TempData["UserMessage"] = new Message() { CssClassName = "alert-success", Title = "Success!", DisplayMessage = "Data Saved" };
return RedirectToAction("Success");
}
catch(Exception e)
{
TempData["UserMessage"] = new Message() { CssClassName = "alert-danger", Title = "Error!", DisplayMessage = "Some Error Occured" };
return RedirectToAction("Error");
}
}
return View(vm); //return to view with model if model is not valid
}
I have a tendency to store my errors and successes in the same array/object and pass them to the view.
Since most of my error/success message would appear in the same spot and they usually don't occur at the same time, this is usually not a problem.
I have a function called ShowFeedback()
that is called as a usercontrol and does the logic to determine what to show. The errors and successes are marked up the same in HTML and only the css differs a bit. So you could have
<div id="feedback" class="error">
Your error message
</div>
or
<div id="feedback" class="success">
Your success message
</div>
精彩评论