使用Htmlhelper,創建文本框TextBox


下面通過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是一個泛型方法,它會給你一個編譯的錯誤,如果你指定的屬性名字是錯誤的,或者屬性的名字發生了改變。(所提供的視圖,在運行的時候,不會編譯)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM