记录学习MVC过程,HTML铺助类(二)
生活随笔
收集整理的這篇文章主要介紹了
记录学习MVC过程,HTML铺助类(二)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.HTML鋪助實(shí)現(xiàn)鏈接(如果不是aspx是mvc <%:%>改成@即可)
第一種:<a href="/User/Index">鏈接到User/Index</a><br />//當(dāng)路由規(guī)則(App_Start文件夾下的RouteConfig)發(fā)生變化時(shí)(比如/變成-)這種方法就失效了第二種:<a href="<%:Url.Action("Index","User") %>">鏈接</a><br />跳轉(zhuǎn)到User控制器下的Index方法第三種:<%:Html.ActionLink("鏈接不帶參","Index","User",new {name="itcast"}) %><br /><%:Html.ActionLink("鏈接不帶參","Index","User") %><br/>
2.HTML鋪助方法實(shí)現(xiàn)一個(gè)下拉框
public ActionResult HtmlHelper(){ViewBag.city = new List<SelectListItem>(){new SelectListItem(){Selected =true,Text = "北京",Value = "1"},new SelectListItem(){Selected =false,Text = "上海",Value = "2"},new SelectListItem(){Selected =false,Text = "廣州",Value = "3"}};ViewBag.demo = "你好";return View();}前臺(tái)頁(yè)面
<div><%:Html.DropDownList("city") %> //HTML鋪助方法會(huì)自動(dòng)幫你把名字相同的進(jìn)行匹配,比如city和ViewBag.City//ViewBage.name是ViewData["name"]的一個(gè)動(dòng)態(tài)封裝器<%:Html.TextBox("demo") %><br />男<%:Html.RadioButton("gender","1",false) %>女<%:Html.RadioButton("gender","0",true) %></div>
3.重寫自己的HTML鋪助方法
定義一個(gè)類,myhtmlhelper,擴(kuò)展htmlhelper方法?靜態(tài)類,靜態(tài)方法,this關(guān)鍵字
public static class myhtmlhelper{public static string myLabel(this HtmlHelper helper, string txt){//要被編碼化<span>屬于html標(biāo)簽被編碼了,就不死html標(biāo)簽是一個(gè)txt了return string.Format("<span>{0}</span>",txt);}public static HtmlString myhtmlstringLabel(this HtmlHelper helper, string txt){//不被編碼化return new HtmlString(string.Format("<span>{0}</span>", txt));}}<%:Html.myhtmlstringLabel("我的擴(kuò)展方法") %>//調(diào)用的時(shí)候會(huì)發(fā)現(xiàn)需要引用System.Web.Mvc
namespace System.Web.Mvc/*命名空間改成引用的命名空間,前臺(tái)頁(yè)面就無(wú)需引用了*/ {/// <summary>/// 擴(kuò)展htmlhelper方法/// </summary>public static class myhtmlhelper{public static string myLabel(this HtmlHelper helper, string txt){//要被編碼化return string.Format("<span>{0}</span>",txt);}public static HtmlString myhtmlstringLabel(this HtmlHelper helper, string txt){//不被編碼化return new HtmlString(string.Format("<span>{0}</span>", txt));}} }
4......................繼續(xù)學(xué)習(xí)中,發(fā)現(xiàn)MVC和webform其實(shí)有很多相似處
?
轉(zhuǎn)載于:https://www.cnblogs.com/jsdvkm/p/4574692.html
總結(jié)
以上是生活随笔為你收集整理的记录学习MVC过程,HTML铺助类(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Could not parse conf
- 下一篇: 2017年html5行业报告,云适配发布