给视图增加帮助器有两种方法:
一种是使用静态方法:
创建:
using System;
namespace MvcApplication1.Helpers
{
public class LabelHelper{public static string Label(string target, string text){return String.Format("", target, text);}}}
调用: <%= LabelHelper.Label("lastName", "Last Name:") %>
第二
种是使用扩展方法,这个更自然,使用起来也更方便:
创建:
using System;
using System.Web.Mvc;
namespace MvcApplication1.Helpers
{
public static class LabelExtensions
{
public static string Label(this HtmlHelper helper, string target, string text)
{
return String.Format("", target, text);
}
}
}
调用: <%= Html.Label("lastName", "Last Name:") %>
模板中值得注意的另一个东西是模板:
定义好一个模板(*.ascx)
其后可以用Html.RenderPartial画出来,这点用好了,会非常好。
这个模板,在我想来相当于可视化的函数。
视图中千万不要加过多的逻辑判断,可以事先在Model中判断好,再显示。如果必须添加判断,一个可选方案是定义帮助器,然后在帮助器中判断。总之保持视图的简洁、简单。
Technorati 标签:
原创
1391

被折叠的 条评论
为什么被折叠?



