博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# ASP.NET MVC HtmlHelper用法大全
阅读量:3557 次
发布时间:2019-05-20

本文共 3766 字,大约阅读时间需要 12 分钟。

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标签的属性集合。使用方法如下。

 

1.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" })%>
 
生成结果为:
这是一个连接
带有QueryString的写法
这是一个连接
这是一个连接
有其它Html属性的写法
这是一个连接
这是一个连接
QueryString与Html属性同时存在
这是一个连接
这是一个连接
 

2.RouteLink

跟ActionLink在功能上一样。
<%=Html.RouteLink("关于", "about", new { })%>
带QueryString
<%=Html.RouteLink("关于", "about", new { page = 1 })%>
<%=Html.RouteLink("关于", "about", new { page = 1 }, new { id = "link1" })%>
 
生成结果:
关于
关于
关于

3.Form   2种方法

<%using(Html.BeginForm("index","home",FormMethod.Post)){%>
<%} %>
 
<%Html.BeginForm("index", "home", FormMethod.Post);//注意这里没有=输出%>
<%Html.EndForm(); %>
 
生成结果:

 

4.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;" })%>
 
生成结果:
 

 

5.TextArea

<%=Html.TextArea("input5", Model.CategoryName, 3, 9,null)%>
<%=Html.TextAreaFor(a => a.CategoryName, 3, 3, null)%>
 
生成结果:
 
6.CheckBox     
<%=Html.CheckBox("chk1",true) %>
<%=Html.CheckBox("chk1", new { @class="checkBox"}) %>
<%=Html.CheckBoxFor(a =>a.IsVaild, new { @class = "checkBox" })%>
 
生成结果:
 
 
 
  7.ListBox
<%=Html.ListBox("lstBox1",(SelectList)ViewData["Categories"])%>
<%=Html.ListBoxFor(a => a.CategoryName, (SelectList)ViewData["Categories"])%>
 
生成结果:
  8.DropDownList
<%= Html.DropDownList("ddl1", (SelectList)ViewData["Categories"], "--Select One--")%>
<%=Html.DropDownListFor(a => a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })%>
 
生成结果:
--Select One--
Beverages
Condiments
Confections
Dairy Products
Grains/Cereals
Meat/Poultry
Produce
Seafood   9.Partial 视图模板 webform里叫自定义控件。功能都是为了复用。但使用上自定义控件真的很难用好。 <% Html.RenderPartial("DinnerForm"); %>  看清楚了没有等号的。

转载地址:http://klsrj.baihongyu.com/

你可能感兴趣的文章
Lua 元表及元方法
查看>>
C#常用的设计模式
查看>>
C#-快速排序算法
查看>>
docker 部署SpringBoot项目
查看>>
mybatis基础知识(四)&输入映射与输出映射
查看>>
gitflow工作流
查看>>
【MongoDB】update修改器($set、$unset、$inc、$push、$pull、$pop)
查看>>
JAVA 继承
查看>>
电脑键盘突然不能打字,很多键变成快捷键了
查看>>
Hbase表映射Hive表三种方法
查看>>
Java中获取List长度
查看>>
this关键字有什么用处?怎么用? 1.访问成员变量,区分成员变量和局部变量。 2.访问成员方法。 3.访问构造方法。 4.返回对当前对象的引用 5.将对当前对象的引用作为参数传递给其他方法。
查看>>
自学sql
查看>>
基于Springboot的社区开发项目
查看>>
nowcoder 左神算法1
查看>>
code刷题
查看>>
左神进阶2窗口
查看>>
dubbo入门
查看>>
http 错误类型
查看>>
一篇文章解决HTTP 请求头!
查看>>