下面通过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的更多相关文章

  1. ASP.NET 动态创建文本框 TextBox (add TextBox to page dynamically)

    下面的函数每执行一次就生成一个TextBox(其实是<input type="Text">)    var i=0;     function changeIt()   ...

  2. word中创建文本框

    word中创建文本框         在插入中点击"文本框"选项卡,例如以下图所看到的:        手工加入自己想要的文本框格式,然后选择所创建的文本框,在工具栏处会发现多了一 ...

  3. HTML创建文本框的3种方式

    我的第一个随笔,记录主要用来整理学习的知识点 1.input 创建单行文本框 <input type="text" size="10" maxlength ...

  4. C#控制文本框(TextBox)只能输入正数,负数,小数

    由于项目需要,需要写一个TextBox文本框,此文本框需要满足:只能输入正数,负数和小数.比如:3,0.3,-4,-0.4等等.        在网上找了许多正则表达式都不好用,由于本人又对正则表达式 ...

  5. [原创]C#应用WindowsApi实现查找(FindWindowEx)文本框(TextBox、TextEdit)。

    /// <summary> /// 获取文本框控件 /// </summary> /// <param name="hwnd">文本框所在父窗口 ...

  6. C#.NET常见问题(FAQ)-如何让文本框textbox内容限制为数字

    //限制文本框的输入 private void txtQuestionScore_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyCha ...

  7. 保留password模式文本框textbox内的数据不丢失。

    在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号),就没有了! protected override ...

  8. easyUI文本框textbox笔记

    知识点: 1.图标位置 Icon Align属性,有left和right两个: 2.textbox的setvalue方法,getvalue方法. <div style="margin: ...

  9. asp.net小技巧:保留password模式文本框textbox内的数据不丢失。

    在asp.net 2.0环境下,使用textbox,提交到服务器再传回,如果textbox是password模式的,那么textbox内的密码(星号.圆点),就没有了! 一个可行的做法是 : prot ...

随机推荐

  1. cocos2dx游戏存储举例及其注意事项

    今天白白跟大家分享一下cocos2dx中游戏的存储及需要注意的事项 cocos2dx中自带了存储类:CCUserDefault ,倘若需要存储的数据量教大的话,建议使用数据库来存储 现在先给大家看一下 ...

  2. matlab中将矩阵按照行打乱顺序的一个例子

    来自百度百科: A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12] rowrank = randperm(size(A, 1)); % 随机打乱的数字,从1~行数打 ...

  3. [Algorithm] Serialize and Deserialize Binary Tree

    Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, ...

  4. 王立平-- android:layout_weight

    效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  5. 绕过Web授权和认证之篡改HTTP请求

    一.什么是HTTP请求 超文本传输协议(HTTP)提供了多种请求方法来与web服务器沟通.当然,大多数方法的初衷是帮助开发者在开发或调试过程中部署和测试HTTP应用.如果服务器配置不当,这些请求方法可 ...

  6. php之快速入门学习-2

    创建(声明)PHP 变量 PHP 没有声明变量的命令. 变量在您第一次赋值给它的时候被创建: <?php $txt="Hello world!"; $x=5; $y=10.5 ...

  7. Speculative Execution in Hadoop

    来自:http://blog.csdn.net/macyang/article/details/7880671 所谓的推测执行,就是当所有task都开始运行之后,Job Tracker会统计所有任务的 ...

  8. 算法笔记_126:算法集训之编程大题集二(Java)

     目录 1 连续数的公倍数 2 漏掉的账目明细 3 罗马数字转十进制 4 逻辑推断 5 平面4点最小距离 6 取球博弈 7 人民币金额大写 8 人员排日程 9 三角螺旋阵 10 手机尾号评分   1 ...

  9. SVN diff 笔记

    SVN diff命令在实际中经常使用,在此记录使用点滴. #对比工作文件与缓存在.svn的“原始”拷贝: svn diff #显示工作文件和服务器版本2的不同: svn diff -r 2 #显示分支 ...

  10. javaweb笔记全套

    Lesson 1 一.eclipse工具的使用 1. java代码的位置 1) 选择工作空间 workspace  选择一个文件夹存放程序(代码) 不要用中文和空格 2) 新建一个java 工程(Pr ...