使用MVC框架中要注意的问题(七):HtmlAttributes
来源:互联网 发布:手机精美时钟软件 编辑:程序博客网 时间:2024/06/05 23:56
在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>
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }我们看到,它是生成标准的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" })%>
也就是说,要通过一个@进行转义。
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- 使用MVC框架中要注意的问题(八):HandleError
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题:何时使用PartialView(部分类)方法
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(一):修改首页以支持主题
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题:如何在页面和用户控件之间传递数据
- MVC中使用aspx当作视图需要注意的问题
- MVC htmlAttributes用法
- Spring MVC 表单提交中需注意的问题
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 使用MVC框架中要注意的问题(四):ActionLink只是执行Get的操作
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 实现SQLServer的Session
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- 使用MVC框架中要注意的问题(八):HandleError
- Uninstalling ASP.NET MVC 1.1 after installing Visual Studio 2010 beta 2
- Enabling .NET 3.5 in SharePoint 2007 Sites, the Lazy Way{转载}
- 如何在javascript中访问页面定义的变量
- Required Content Placeholders in SharePoint Masterpages
- jquery ui中 accordion的问题及我的解决方法
- javascript中类的定义和使用{转载}
- vb没法定位.dll函数入口点