使用Htmlhelper,建立文字框TextBox

來源:互聯網
上載者:User

標籤:

下面通過HtmlHelper協助類,建立文字框。

首先建立一個實體類,做為下面的例子:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MVCRestartlearnning.Models{    public class Student { /// <summary> /// 學號 /// </summary> public int StudentId { get; set; } /// <summary> /// 姓名 /// </summary> public string StudentName { get; set; } /// <summary> /// 年齡 /// </summary> public int Age { get; set; } /// <summary> /// 是否新入學 /// </summary> public bool isNewlyEnrolled { get; set; } /// <summary> /// 密碼 /// </summary> public string Password { get; set; } }}
TextBox();

Html.TextBox()方法,建立文字框【<input type="text"/>】,並且可以帶上name,value和html屬性;

TextBox方法的簽名:

MvcHtmlString Html.TextBox(string name, string value, object htmlAttributes)

TextBox method has many overloads. Please visit MSDN to know all the overloads of TextBox() method.

The TextBox() method is a loosely typed method because name parameter is a string. The name parameter can be a property name of model object. It binds specified property with textbox. So it automatically displays a value of the model property in a textbox and visa-versa.

Example:例子:

  @Html.TextBox("student", null, new { @class="Myclass"}) 

產生:

 <input class="Myclass" id="student" name="student" type="text" value="" />

上面的例子中,第一個參數“student”,被設定成文字框的id屬性和name屬性的值,第一個參數,是用來顯示在文字框裡的值,第三個參數,被用來設定文字框的class屬性了,HtmlAttrbutes是一個物件類型,它是一個匿名對象,屬性名稱字都會以一個@符號開始;

上面的例子中,第一個參數,你也可以換個名字,不用“student”,不過,不會綁定到模型中;

  @Html.TextBox("myTextBox", "hello,TextBox", new { @class="myclasses"})

產生:

背後代碼:

<input class="myclasses" id="myTextBox" name="myTextBox" type="text" value="hello,TextBox" />
TextBoxFor();

TextBoxFor方法是一個強型別的擴充方法,它使用lambda運算式,為模型產生文字框;

TextBoxFor方法,綁定特定的模型屬性到文字框中,所以會自動顯示內容的值到文字框中;

簽名:

MvcHtmlString TextBoxFor(Expression<Func<TModel,TValue>> expression, object htmlAttributes)

Visit MSDN to know all the overloads of TextBoxFor() method.

 

Example:

  @Html.TextBoxFor(m => m.StudentName, new { @class="form-control"}) 

 

產生:

 <input class="form-control" id="StudentName" name="StudentName" type="text" value="" />

:

 

在上面的例子中,TextBoxFor第一個參數是一個lambda運算式,指定這個StudentName屬性,並綁定到文字框中,以其名稱產生了id和name屬性的值,如果StudentName屬性值是Tom ,那麼,文字框中就會顯示Tom;

 

Difference between TextBox and TextBoxFor:
  • @Html.TextBox() is loosely typed method whereas @Html.TextBoxFor() is a strongly typed (generic) extension method.
  • TextBox是松類型的,而TextBoxFor是強型別的擴充方法;
  • TextBox() requires property name as string parameter where as TextBoxFor() requires lambda expression as a parameter.
  • TextBox需要屬性名稱字作為string類型的參數,然而TextBoxFor需要lambda運算式作為參數;
  • TextBox doesn‘t give you compile time error if you have specified wrong property name. It will throw run time exception.
  • 如果你指定了一個錯誤的屬性名稱字,TextBox不會報編譯錯誤,但是會在啟動並執行時候,報運行錯誤;
  • TextBoxFor is generic method so it will give you compile time error if you have specified wrong property name or property name changes. (Provided view is not compile at run time. )
  • TextBoxFor是一個泛型方法,它會給你一個編譯的錯誤,如果你指定的屬性名稱字是錯誤的,或者屬性的名字發生了改變。(所提供的視圖,在啟動並執行時候,不會編譯)

使用Htmlhelper,建立文字框TextBox

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.