下面通过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是一个泛型方法,它会给你一个编译的错误,如果你指定的属性名字是错误的,或者属性的名字发生了改变。(所提供的视图,在运行的时候,不会编译)

最新文章

  1. js兼容方法:事件添加|事件绑定|事件监听 addEvent
  2. 一封给JVM懵懂者的情书【不看错过一生幸福】
  3. Real-Time SQL Monitoring
  4. Bootstrap非常简单实用的web前端开发框架
  5. DXUT初步理解
  6. C++引用变量(转)
  7. VS2010彻底卸载
  8. linux进程间通信方式
  9. js关键字
  10. js一些方法的扩展
  11. OpenStack学习推荐
  12. Python中逗号作用的实例分析
  13. 最新Connectify注冊码(序列号) Connectify3.7序列号 破解版
  14. codevs1690开关灯
  15. liunx下NetworkManager导致网卡不能启动
  16. c++ anonymous union,struct -- 匿名联合体和机构体
  17. ZOJ 3542 2011大连现场赛D题(简单模拟)
  18. iOS开发中数据持久化
  19. 学python走过的坑 三 不能实现的浏览器缩放功能
  20. PHP 【六】

热门文章

  1. IOS中键盘隐藏几种方式
  2. Splunk的安装与使用
  3. Android -- Drawable &amp;&amp; Bitmap
  4. 解决Sqlserver 2008 R2在创建登录名出错&quot;此版本的 Microsoft Windows 不支持 MUST_CHANGE 选项。 (Microsoft SQL Server,错误: 15195)&quot;
  5. SIT/UAT测试
  6. leetcode第一刷_Integer to Roman
  7. AIDL旅行记之开篇AIDL基本介绍
  8. GP开发示例:数据库去重
  9. Ubuntu12.04+OpenERP7.0安装笔记
  10. springboot EnableAutoConfiguration