最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

自定义ViewHelper来简化MVC view的开发

日期:09-13    来源:中国设计秀    作者:cnwebshow.com

 接上篇,我们下面通过HtmlFiledSet helper来展示何时使用EndView():P55中国设计秀
P55中国设计秀
    如果你用asp.net已经有一段时间了,那使用Html.BeginForm helper来创建HTML form标签的方式会让你觉得有点怪.当你创建一个新的Asp.net mvc项目后,在View里的ChangePassword.aspx会默认被创建,这个页面使用了Html.BeginForm helper,下面是使用这个helper的代码段:P55中国设计秀
P55中国设计秀
<% using (Html.BeginForm()) { %> P55中国设计秀
    <div> P55中国设计秀
        <fieldset> P55中国设计秀
            <legend>Account Information</legend> P55中国设计秀
            <p> P55中国设计秀
                <label for="currentPassword">Current password:</label> P55中国设计秀
                <%= Html.Password("currentPassword") %> P55中国设计秀
                <%= Html.ValidationMessage("currentPassword") %> P55中国设计秀
            </p> P55中国设计秀
            <p> P55中国设计秀
                <label for="newPassword">New password:</label> P55中国设计秀
                <%= Html.Password("newPassword") %> P55中国设计秀
                <%= Html.ValidationMessage("newPassword") %> P55中国设计秀
            </p> P55中国设计秀
            <p>                 <label for="confirmPassword">Confirm new password:</label> P55中国设计秀
                <%= Html.Password("confirmPassword") %> P55中国设计秀
                <%= Html.ValidationMessage("confirmPassword") %> P55中国设计秀
            </p> P55中国设计秀
            <p> P55中国设计秀
                <input type="submit" value="Change Password" /> P55中国设计秀
            </p> P55中国设计秀
        </fieldset> P55中国设计秀
    </div> P55中国设计秀
<% } %> P55中国设计秀
上面代码中你会发现Html.BeginForm的使用和<%=Html.Password(“currentPassword”) %>的使用不尽相同,也就是Html.BeginForm在using语句中被调用,这点很有意思,让我先来看看上面代码段生成后的Html,如下:P55中国设计秀
P55中国设计秀
<form  P55中国设计秀
  action="/Account/LogOn?ReturnUrl=%2fAccount%2fChangePassword"  P55中国设计秀
  method="post"> P55中国设计秀
  <div> P55中国设计秀
    <fieldset> P55中国设计秀
      <legend>Account Information</legend> P55中国设计秀
      <p> P55中国设计秀
        <label for="username">Username:</label> P55中国设计秀
        <input id="username" name="username" type="text" value="" />   P55中国设计秀
      </p> P55中国设计秀
      <p> P55中国设计秀
        <label for="password">Password:</label> P55中国设计秀
        <input id="password" name="password" type="password" />   P55中国设计秀
      </p> P55中国设计秀
      <p> P55中国设计秀
        <input id="rememberMe" name="rememberMe" type="checkbox" value="true" /> P55中国设计秀
        <input name="rememberMe" type="hidden" value="false" /> P55中国设计秀
        <label class="inline" for="rememberMe">Remember me?</label> P55中国设计秀
      </p> P55中国设计秀
      <p> P55中国设计秀
        <input type="submit" value="Log On" /> P55中国设计秀
      </p> P55中国设计秀
    </fieldset> P55中国设计秀
  </div> P55中国设计秀
</form> 比较<%= Html.Password("currentPassword") %> 语句和Html.BeginForm所生成的HTML代码你会发现password那段仅仅仅仅将调用password扩展方法变成对应的Html,而BeginForm调用HTML扩展方法来注入form的开始标签,<form…>,和using语句结束时(“}”)注入标签<./form>.这种方式十分方便,因为我们可以一方面使用view helper创建合适的form标签,另一方面在form标签内插入任何我们想插入的html.这种方法的工作原理是当你调用Html.BeginForm方法并返回MvcForm类型的对象,这个对象在using语句中所以当对象被回收(译者按:也就是Dispose)的时候,也就是执行到结尾的”<% } %>”时,关闭标签</form>将会被写入到View中.下面是Dispose方法的实现:P55中国设计秀
P55中国设计秀
public void Dispose() { P55中国设计秀
    Dispose(true /* disposing */); P55中国设计秀
    GC.SupPRessFinalize(this); P55中国设计秀
} P55中国设计秀
P55中国设计秀
protected virtual void Dispose(bool disposing) { P55中国设计秀
    if (!_disposed) { P55中国设计秀
        _disposed = true; P55中国设计秀
        _httpResponse.Write("</form>"); P55中国设计秀
    } P55中国设计秀
}  P55中国设计秀
P55中国设计秀
我们用相似的方法来实现HtmlFieldSet:P55中国设计秀
P55中国设计秀
从这篇文章附带的代码中有一个”DetailsClassic.aspx”页面中,一些字段包含于fieldset元素中,具体代码如下:P55中国设计秀
P55中国设计秀
<fieldset class="details-field-group" name="Details"> P55中国设计秀
  <legend>Details</legend> P55中国设计秀
  <ol> P55中国设计秀
    <li> P55中国设计秀
      <label for="FirstName">FirstName</label> P55中国设计秀
      <span id="FirstName"> P55中国设计秀
        <%= Html.Encode(Model.FirstName) %></span>  P55中国设计秀
    </li> P55中国设计秀
    <li> P55中国设计秀
      <label for="LastName">LastName</label> P55中国设计秀
      <span id="LastName"><%= Html.Encode(Model.LastName) %></span> P55中国设计秀
    </li> P55中国设计秀
    <li> P55中国设计秀
      <label for="Email">Email</label> P55中国设计秀
      <span id="Email"><%= Html.Encode(Model.Email) %></span> P55中国设计秀
    </li> P55中国设计秀
    <li> P55中国设计秀
      <label for="Phone">Phone</label> P55中国设计秀
      <span id="Phone"><%= Html.Encode(Model.Phone) %></span> P55中国设计秀
    </li> P55中国设计秀
    <li> P55中国设计秀
      <label for="Gender">Gender</label> P55中国设计秀
      <span id="Gender"><%= Html.Encode(Model.Gender) %></span> P55中国设计秀
    </li> P55中国设计秀
  </ol> P55中国设计秀
</fieldset> 如果能用一个view helper来创建下面所有的html标签,那会惬意很多:P55中国设计秀
P55中国设计秀
fieldset的开始标记 P55中国设计秀
legend标签 P55中国设计秀
ol的开始标签 P55中国设计秀
ol的结束标签 P55中国设计秀
fieldset的结束标签 P55中国设计秀
如果能用view helper来处理这些,那上面那段代码无疑会简单很多,尤其再加上使用Html.Text标签来代替上面的text域。让我们开始做到这一点,首先创建一个实现IViewObject接口的类,命名为HtmlFieldSet类并继承与AbstractHtmlViewObject,整个类的代码附下:P55中国设计秀
P55中国设计秀
public class HtmlFieldset : AbstractHtmlViewObject P55中国设计秀
{ P55中国设计秀
    private readonly string mTitle; P55中国设计秀
P55中国设计秀
    public HtmlFieldset( P55中国设计秀
        ViewRequestContext requestContext, string name,  P55中国设计秀
        string title, object attributes) P55中国设计秀
        : base(requestContext, name) P55中国设计秀
    { P55中国设计秀
        mTitle = title; P55中国设计秀
        Attributes = attributes; P55中国设计秀
    } P55中国设计秀
P55中国设计秀
    private TagBuilder FieldsetTag { get; set; } P55中国设计秀
P55中国设计秀
    private TagBuilder OlTag { get; set; } P55中国设计秀
P55中国设计秀
    public override void StartView() P55中国设计秀
    { P55中国设计秀
        HttpResponseBase httpResponse = RequestContext.HttpResponse; P55中国设计秀
P55中国设计秀
        FieldsetTag = new TagBuilder("fieldset"); P55中国设计秀
P55中国设计秀
        // apply any Attributes passed in P55中国设计秀
        if (Attributes != null) P55中国设计秀
        { P55中国设计秀
            FieldsetTag.MergeAttributes(new RouteValueDictionary(Attributes)); P55中国设计秀
        }  P55中国设计秀
        // The Name property should override any passed into the Attributes P55中国设计秀
        FieldsetTag.MergeAttribute("name", Name, true); P55中国设计秀
P55中国设计秀
        httpResponse.Write(FieldsetTag.ToString(TagRenderMode.StartTag)); P55中国设计秀
P55中国设计秀
        if (!string.IsNullOrEmpty(mTitle)) P55中国设计秀
        { P55中国设计秀
            TagBuilder legendTag = new TagBuilder("legend"); P55中国设计秀
            legendTag.SetInnerText(mTitle); P55中国设计秀
            httpResponse.Write(legendTag.ToString(TagRenderMode.Normal)); P55中国设计秀
        } P55中国设计秀
P55中国设计秀
        OlTag = new TagBuilder("ol"); P55中国设计秀
        httpResponse.Write(OlTag.ToString(TagRenderMode.StartTag)); P55中国设计秀
    } P55中国设计秀
P55中国设计秀
    public override void EndView() P55中国设计秀
    { P55中国设计秀
        HttpResponseBase httpResponse = RequestContext.HttpResponse; P55中国设计秀
P55中国设计秀
        httpResponse.Write(OlTag.ToString(TagRenderMode.EndTag)); P55中国设计秀
        httpResponse.Write(FieldsetTag.ToString(TagRenderMode.EndTag)); P55中国设计秀
    } P55中国设计秀
} P55中国设计秀
这个类的实现和其它的view对象没什么不同,除了EndView方法,在这里我们需要EndView方法来生成必须的结束标记。当然我们也可以使用EndView来生成任何需要的HTML,在这里我们仅是用它生成结束标记。P55中国设计秀
P55中国设计秀
-------------------------------------------P55中国设计秀
P55中国设计秀
待续…P55中国设计秀
P55中国设计秀
原文链接:http://mvcviewhelpers.codeplex.com/P55中国设计秀
P55中国设计秀
translated by CareySonP55中国设计秀

本文引用地址:/bc/article_46179.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明