Rya*_*anW 5 asp.net asp.net-mvc
我正在加速ASP.NET MVC,看看我如何在视图中输出消息.做这样的事情最好的方法是什么?助手?控制?或者就是这样?
<% if (ViewData.ContainsKey("message") && !string.IsNullOrEmpty(ViewData["message"].ToString())) { %>
<div class="notice">
<%= ViewData["message"] %>
</div>
<% } %>
Run Code Online (Sandbox Code Playgroud)
我会使用一个HTML帮助器:
public static class HtmlExtensions
{
public static string GetMessage(this HtmlHelper htmlHelper)
{
var message = htmlHelper.ViewData["message"] as string;
if (string.IsNullOrEmpty(message))
{
return string.Empty;
}
var builder = new TagBuilder("div");
builder.AddCssClass("notice");
builder.SetInnerText(message);
return builder.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
并在视图中:
<%= Html.GetMessage() %>
Run Code Online (Sandbox Code Playgroud)
备注:如果您决定按原样使用代码,请不要忘记对消息的值进行html编码.
| 归档时间: |
|
| 查看次数: |
623 次 |
| 最近记录: |