使用MVC框架中要注意的问题(七):HtmlAttributes

使用MVC框架中要注意的问题(七):HtmlAttributes

在MVC的View中,我们可以通过HtmlHelper的一些扩展方法插入一些控件,例如通过Html.TextBox插入一个文本框等等,下面是一个简单的范例

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    创建相册
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="widget">
        <div class="title">
            <div class="icon">
            </div>
            <h3>
                创建新相册
            </h3>
        </div>
        <div class="details">
            <%using (Html.BeginForm())
              {
            %>
            <fieldset>
                <legend>基本信息</legend>
                <p>
                    <label for="title">
                        标题:</label>
                    <%= Html.TextBox("title")%>
                    <%= Html.ValidationMessage("title") %>
                </p>
                <p>
                    <label for="desc">
                        描述:</label>
                    <%= Html.TextArea("desc")%>
                    <%= Html.ValidationMessage("desc") %>
                </p>
                <p>
                    <label for="public">
                        是否公开:</label>
                    <%= Html.CheckBox("public") %>
                </p>

                <p>
                    <input type="submit" value="提交" />
                </p>
            </fieldset>
            <%
                } %>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server">
</asp:Content>
看起来不错对吧。要注意的是,这并不是控件,而是一个方法。那么,它会生成什么样的代码呢?
<div class="details">
            <form action="/Gallery/Create" method="post">
            <fieldset>
                <legend>基本信息</legend>
                <p>
                    <label for="title">
                        标题:</label>
                    <input id="title" name="title" type="text" value="" />
                    
                </p>
                <p>
                    <label for="desc">
                        描述:</label>
                    <textarea cols="20" id="desc" name="desc" rows="2">
</textarea>
                    
                </p>
                <p>
                    <label for="public">
                        是否公开:</label>
                    <input id="public" name="public" type="checkbox" value="true" /><input name="public" type="hidden" value="false" />
                </p>
 
                <p>
                    <input type="submit" value="提交" />
                </p>
            </fieldset>
            </form>
        </div>

我们看到,它是生成标准的HTML控件。

那么如果我们需要对该控件进行定义呢?例如设置宽度,颜色等等,怎么办呢?这就是所谓的htmlAttributes起作用的地方。请注意下面粗体的部分

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    创建相册
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="widget">
        <div class="title">
            <div class="icon">
            </div>
            <h3>
                创建新相册
            </h3>
        </div>
        <div class="details">
            <%using (Html.BeginForm())
              {
            %>
            <fieldset>
                <legend>基本信息</legend>
                <p>
                    <label for="title">
                        标题:</label>
<%= Html.TextBox("title", "初始值", new { width = "400px", style = "background-color:red" })%>
                    <%= Html.ValidationMessage("title") %>
                </p>
                <p>
                    <label for="desc">
                        描述:</label>
                    <%= Html.TextArea("desc")%>
                    <%= Html.ValidationMessage("desc") %>
                </p>
                <p>
                    <label for="public">
                        是否公开:</label>
                    <%= Html.CheckBox("public") %>
                </p>

                <p>
                    <input type="submit" value="提交" />
                </p>
            </fieldset>
            <%
                } %>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server">
</asp:Content>
也就是说,我们可以通过匿名类型的方式任意构造出来一些属性。当然,这些都是Input标签默认就支持的属性。事实上,哪怕不支持也没有关系,直接写不会出错的。
那么,如果我们希望添加一个css类的绑定呢,要特别注意一下,因为class是一个c#关键字,你可能需要下面这样指定
<%= Html.TextArea("desc", "初始值", new { @class = "myclass" })%>
也就是说,要通过一个@进行转义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值