MVC-HtmlHelper的作用

 1.@Html.ActionLink("列表", "Index") 可以根据路由自动生成相应的超链接

当路由表如下时:

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

生成的超链接为:<a href="/UserInfo">列表</a>

当路由表如下时:

 routes.MapRoute(
                name: "Default",
                url: "{controller}-{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

生成的超链接为:<a href="/UserInfo-Index">列表</a>

 @Html.ActionLink("列表2", "Index", new { name = "zhangsan", age = "12" }, new { id = "a2" }) 对应生成的超链接为:

<a href="/UserInfo?name=zhangsan&amp;age=12" id="a2">列表2</a>

 @Html.ActionLink("列表3", "Index", new { name = "zhangsan", age = "12" }, new { id = "a2",@class="txt" })为a标签添加类要用@class

 

2 @Html.Raw("<span style='color:red'>asp.net mvc</span>")

     原样输出asp.net mvc
3   @Html.Encode("<span style = 'color:red'> asp.net mvc </span >")

     编码后输出&lt;span style = &#39;color:red&#39;&gt; asp.net mvc &lt;/span &gt;

4.  @Html.DropDownList("city") id,name属性都是city

public ActionResult Register()
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = "北京", Value = "1" , Selected=true});
            list.Add(new SelectListItem { Text = "深圳", Value = "2" });
            list.Add(new SelectListItem { Text = "上海", Value = "3" });
            ViewData["city"] = list; //名字和视图下拉框名字相同
            return View();
        }

 @Html.DropDownList("city",null,new { id="selectCity"})  填充数据,并更改id属性

5  
        男 @Html.RadioButton("sex","0",true,new { id = "male" })
        女 @Html.RadioButton("sex","1")

6  @using (Html.BeginForm())
        {
        <input type="submit" name="name" value="submit" />
        }

默认生成的是form的action还是当前页对应的方法,方法为post

<form action="/UserInfo/Register" method="post">

<input type="submit" name="name" value="submit" />

</form>

 @using(Html.BeginForm("Index","UserInfo",FormMethod.Get,new { id = "frm"}))
        {
            <input type="submit" name="name" value="submit" />
        }

生成的是form表单如下:

<form action="/UserInfo" id="frm" method="get">

      <input type="submit" name="name" value="submit" />

</form>

7  @Html.TextBox("txtName","请输入名字",new { id="UserName"})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值