How do I provide success messages in asp.net mvc?
如何在asp.net mvc中提供成功消息?
5 个解决方案
#1
16
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:
如果在不同的页面上显示消息,那么ViewData不会对您有所帮助,因为每个请求都会重新初始化消息。另一方面,TempData可以为两个请求存储数据。这里有一个例子:
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
.
在if块内部,您必须使用TempData,因为您正在执行重定向(另一个请求),但是在其他内部,您可以使用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>
}
#2
12
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>
#3
2
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.
使用ViewData存储成功消息。在控制器中创建成功消息,并在视图中检查它。如果存在,渲染它。
#4
1
TempData can be used like a dictionary. Each saved value lasts for the current and the next request. Perfect for redirects.
TempData可以像字典一样使用。每个保存的值持续到当前和下一个请求。适合重定向。
this.TempData["messages"] = "Success!";
return RedirectToAction("YourAction");
#5
0
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
我有一个名为ShowFeedback()的函数,它被称为usercontrol,并执行逻辑来确定要显示什么。错误和成功在HTML中标记为相同的,只有css稍有不同。所以你可以有
<div id="feedback" class="error">
Your error message
</div>
or
或
<div id="feedback" class="success">
Your success message
</div>
#1
16
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:
如果在不同的页面上显示消息,那么ViewData不会对您有所帮助,因为每个请求都会重新初始化消息。另一方面,TempData可以为两个请求存储数据。这里有一个例子:
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
.
在if块内部,您必须使用TempData,因为您正在执行重定向(另一个请求),但是在其他内部,您可以使用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>
}
#2
12
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>
#3
2
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.
使用ViewData存储成功消息。在控制器中创建成功消息,并在视图中检查它。如果存在,渲染它。
#4
1
TempData can be used like a dictionary. Each saved value lasts for the current and the next request. Perfect for redirects.
TempData可以像字典一样使用。每个保存的值持续到当前和下一个请求。适合重定向。
this.TempData["messages"] = "Success!";
return RedirectToAction("YourAction");
#5
0
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
我有一个名为ShowFeedback()的函数,它被称为usercontrol,并执行逻辑来确定要显示什么。错误和成功在HTML中标记为相同的,只有css稍有不同。所以你可以有
<div id="feedback" class="error">
Your error message
</div>
or
或
<div id="feedback" class="success">
Your success message
</div>