ASP.net MVC--Htmlhelper

來源:互聯網
上載者:User

標籤:mvc htmlhelper

(一)引言

HtmlHelper用來在視圖中呈現 HTML 控制項。


我們在開發前台和後台互動的時候,綁定資料會很讓人頭疼,現在我們用MVC幫我們寫好了的Htmlhelper來幫我們綁定資料。


用一張圖來總結一下HtmlHelper:



我就寫咱們最常用的幾個吧。


(二)具體Dome


1、TextBox


我們在前台的視圖中可以寫這樣的代碼:


<span style="font-family:KaiTi_GB2312;font-size:18px;">@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;" })</span>

產生的HTML代碼是這樣的:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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" /></span>

TextArea


<span style="font-family:KaiTi_GB2312;font-size:18px;">@Html.TextArea("input5", Model.CategoryName, 3, 9,null)@Html.TextAreaFor(a => a.CategoryName, 3, 3, null)</span>

產生代碼:


<span style="font-family:KaiTi_GB2312;font-size:18px;"><textarea cols="9" id="input5" name="input5" rows="3">Beverages</textarea><textarea cols="3" id="CategoryName" name="CategoryName" rows="3">Beverages</textarea></span>

2、DropDownList


<span style="font-family:KaiTi_GB2312;font-size:18px;">@Html.DropDownList("CId",ViewBag.selList as IEnumerable<SelectListItem>)</span>

效果:


是一個下拉框,從資料庫中根據“CId”查詢,selList是從後台傳來的代碼。


後台中的查詢:


<span style="font-family:KaiTi_GB2312;font-size:18px;">        public ActionResult Index()        {            //1.1查詢班級資料            List<Models.Class> listCla = db.Classes.Where(c => c.CIsDel == false).ToList();//.Select(c => new SelectListItem() { });            //1.2將班級資料封裝到 SelectList中,並指定 要產生下拉框選項的 value 和 text 屬性            SelectList selList = new SelectList(listCla, "CId", "CName");            //1.3調用 SelectList 的 As 方法,自動產生 SelectListItem集合,並存入ViewBag            ViewBag.selList = selList.AsEnumerable();            //1.4載入視圖            return View();        } </span>

用ViewData擷取後台傳來的數組。


3、Form


有兩種用法,


<span style="font-family:KaiTi_GB2312;font-size:18px;">@using (Html.BeginForm("Login", "User", FormMethod.Post, new { id = "form1" })){    內容    }</span>

<span style="font-family:KaiTi_GB2312;font-size:18px;">@Html.BeginForm("Login", "User", FormMethod.Post, new { id="form2" })    <input type="text" />@{Html.EndForm();}</span>

這兩種都是可以的,但是我們一般會用第一種,因為第一種在結構上簡單,好用。


舉個例子:


<span style="font-family:KaiTi_GB2312;font-size:18px;"><h2>BeginForm{}建立表單標籤 -- 推薦</h2>@using (Html.BeginForm("Login", "User", FormMethod.Post, new { id = "form1" })){    @Html.TextBox("txtName", "我是文字框", new { style="border:1px solid #0094ff;" });<br />    }</span>


效果:



是不是看起來就很清楚了,這樣我們就可以將文字框中的資料提交到Login/User(控制器/視圖)用Post方法提交了。


4、Link


ActionLink--連結到操作方法。


<span style="font-family:KaiTi_GB2312;font-size:18px;"><h2>Html.ActionLink 產生超連結</h2>@Html.ActionLink("我是超連結", "Part", "Home", new { id = "btnLink",style = "border:1px solid #0094ff"})</span>

效果是:



當點選連結的時候,我們就到了Home/Part(控制器/視圖)連結了。


(三)總結


這個部分,只是MVC中很小的一部分,但是就是這很小的部分,是聯絡了後台和前台資料。










ASP.net MVC--Htmlhelper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.