C# ASP.NET MVC HtmlHelper用法大全 (转)

简介: HTML扩展类的所有方法都有2个参数: 以textbox为例子 public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary htmlAttributes )public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object htmlAttributes ) 这2个参数代表这个html标签的属性集合。
HTML扩展类的所有方法都有2个参数:
以textbox为例子
 
  
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, IDictionary < string , Object > htmlAttributes )
public static string TextBox( this HtmlHelper htmlHelper, string name, Object value, Object htmlAttributes )
这2个参数代表这个html标签的属性集合。使用方法如下。
ActionLink
 
  
<% = Html.ActionLink( " 这是一个连接 " , " Index " , " Home " ) %>
带有QueryString的写法
<% = Html.ActionLink( " 这是一个连接 " , " Index " , " Home " , new { page = 1 }, null ) %>
<% = Html.ActionLink( " 这是一个连接 " , " Index " , new { page = 1 }) %>
有其它Html属性的写法
<% = Html.ActionLink( " 这是一个连接 " , " Index " , " Home " , new { id = " link1 " }) %>
<% = Html.ActionLink( " 这是一个连接 " , " Index " , null , new { id = " link1 " }) %>
QueryString与Html属性同时存在
<% = Html.ActionLink( " 这是一个连接 " , " Index " , " Home " , new { page = 1 }, new { id = " link1 " }) %>
<% = Html.ActionLink( " 这是一个连接 " , " Index " , new { page = 1 }, new { id = " link1 " }) %>
生成结果为:
< a href ="/" > 这是一个连接 </ a >
带有QueryString的写法
< a href ="/?page=1" > 这是一个连接 </ a >
< a href ="/?page=1" > 这是一个连接 </ a >
有其它Html属性的写法
< a href ="/?Length=4" id ="link1" > 这是一个连接 </ a >
< a href ="/" id ="link1" > 这是一个连接 </ a >
QueryString与Html属性同时存在
< a href ="/?page=1" id ="link1" > 这是一个连接 </ a >
< a href ="/?page=1" id ="link1" > 这是一个连接 </ a >
RouteLink
 
  
跟ActionLink在功能上一样。
<% = Html.RouteLink( " 关于 " , " about " , new { }) %>
带QueryString
<% = Html.RouteLink( " 关于 " , " about " , new { page = 1 }) %>
<% = Html.RouteLink( " 关于 " , " about " , new { page = 1 }, new { id = " link1 " }) %>
生成结果:
< a href ="/about" > 关于 </ a >
< a href ="/about?page=1" > 关于 </ a >
< a href ="/about?page=1" id ="link1" > 关于 </ a >
Form 2种方法
 
  
<% using(Html.BeginForm( " index " , " home " ,FormMethod.Post)){ %>
<% } %>
<% Html.BeginForm( " index " , " home " , FormMethod.Post); // 注意这里没有 = 输出 %>
<% Html.EndForm(); %>
生成结果:
< form action ="/home/index" method ="post" ></ form >
TextBox , Hidden
 
  
<% = Html.TextBox( " input1 " ) %>
<% = Html.TextBox( " input2 " ,Model.CategoryName, new { @style = " width:300px; " }) %>
<% = Html.TextBox( " input3 " , ViewData[ " Name " ], new { @style = " width:300px; " }) %>
<% = Html.TextBoxFor(a => a.CategoryName, new { @style = " width:300px; " }) %>
生成结果:
< input id ="input1" name ="input1" type ="text" value ="" />
< input id ="input2" name ="input2" style ="width:300px;" type ="text" value ="Beverages" />
< input id ="input3" name ="input3" style ="width:300px;" type ="text" value ="" />
< input id ="CategoryName" name ="CategoryName" style ="width:300px;" type ="text" value ="Beverages" />
TextArea
 
  
<% = Html.TextArea( " input5 " , Model.CategoryName, 3 , 9 , null ) %>
<% = Html.TextAreaFor(a => a.CategoryName, 3 , 3 , null ) %>
生成结果:
< textarea cols ="9" id ="input5" name ="input5" rows ="3" > Beverages </ textarea >
< textarea cols ="3" id ="CategoryName" name ="CategoryName" rows ="3" > Beverages </ textarea >
CheckBox
 
  
<% = Html.CheckBox( " chk1 " , true ) %>
<% = Html.CheckBox( " chk1 " , new { @class = " checkBox " }) %>
<% = Html.CheckBoxFor(a => a.IsVaild, new { @class = " checkBox " }) %>
生成结果:
< input checked ="checked" id ="chk1" name ="chk1" type ="checkbox" value ="true" />
< input name ="chk1" type ="hidden" value ="false" />
< input class ="checkBox" id ="chk1" name ="chk1" type ="checkbox" value ="true" />
< input name ="chk1" type ="hidden" value ="false" />
< input checked ="checked" class ="checkBox" id ="IsVaild" name ="IsVaild" type ="checkbox" value ="true" />
< input name ="IsVaild" type ="hidden" value ="false" />
ListBox
 
  
<% = Html.ListBox( " lstBox1 " ,(SelectList)ViewData[ " Categories " ]) %>
<% = Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData[ " Categories " ]) %>
生成结果:
< select id ="lstBox1" multiple ="multiple" name ="lstBox1" >
< option value ="1" > Beverages </ option >
< option value ="2" > Condiments </ option >
< option selected ="selected" value ="3" > Confections </ option >
< option value ="4" > Dairy Products </ option >
< option value ="5" > Grains/Cereals </ option >
< option value ="6" > Meat/Poultry </ option >
< option value ="7" > Produce </ option >
< option value ="8" > Seafood </ option >
</ select >
< select id ="CategoryName" multiple ="multiple" name ="CategoryName" >
< option value ="1" > Beverages </ option >
< option value ="2" > Condiments </ option >
< option value ="3" > Confections </ option >
< option value ="4" > Dairy Products </ option >
DropDownList
 
  
<% = Html.DropDownList( " ddl1 " , (SelectList)ViewData[ " Categories " ], " --Select One-- " ) %>
<% = Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData[ " Categories " ], " --Select One-- " , new { @class = " dropdownlist " }) %>
生成结果:
< select id ="ddl1" name ="ddl1" >
< option value ="" > --Select One-- </ option >
< option value ="1" > Beverages </ option >
< option value ="2" > Condiments </ option >
< option selected ="selected" value ="3" > Confections </ option >
< option value ="4" > Dairy Products </ option >
< option value ="5" > Grains/Cereals </ option >
< option value ="6" > Meat/Poultry </ option >
< option value ="7" > Produce </ option >
< option value ="8" > Seafood </ option >
</ select >
< select class ="dropdownlist" id ="CategoryName" name ="CategoryName" >
< option value ="" > --Select One-- </ option >
< option value ="1" > Beverages </ option >
< option value ="2" > Condiments </ option >
< option value ="3" > Confections </ option >
< option value ="4" > Dairy Products </ option >
< option value ="5" > Grains/Cereals </ option >
< option value ="6" > Meat/Poultry </ option >
< option value ="7" > Produce </ option >
< option value ="8" > Seafood </ option >
</ select >
Partial 视图模板
 
  
webform里叫自定义控件。功能都是为了复用。但使用上自定义控件真的很难用好。
<% Html.RenderPartial( " DinnerForm " ); %>
目录
相关文章
|
19天前
|
C#
c#中switch case语句的用法
C#中的 `switch case`语句提供了一种简洁而高效的方式来处理多个条件分支。通过了解其基本语法、注意事项和高级用法,可以在实际开发中灵活运用 `switch case`,提高代码的可读性和维护性。希望本文能帮助你更好地理解和使用C#中的 `switch case`语句。
47 0
|
3月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
46 7
|
3月前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
74 0
|
4月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
55 0
|
4月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
4月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
142 0
|
7月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
211 0
|
7月前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
83 0
|
7月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
362 5
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
173 0