@Html.RouteLink("LinkText", new { action = "ActionName", id = 1 })
Input 输入框的使用-TextBox
@Html.TextBox ("NameId") @Html.TextBox("NameId", "Value") @Html.TextBox("NameId", "Value", new { @class = "classText "ControllerName" }) @Html.RouteLink("LinkText", new { action = "ActionName", id = 1 }) 生成html源码 @Html.TextBox @Html.TextBox("NameId") @Html.TextBox("NameId", "Value") @Html.TextBox("NameId", "Value", new { @class , list) @Html.BeginForm @using (Html.BeginForm("PostData", "Html", FormMethod.Get)) { ; @Html.TextBoxViewBag.Price=10.0; return View(); } 在相应的视图中,使用 ViewBag 中的值来为 TextBox 辅助方法命名,可以实现渲染显示价格的文本框: @Html.TextBox ActionResult Edit(int id){ ViewBag.Album = new Album{Price=11}; return View(); } 在视图中使用代码: @Html.TextBox ActionResult Edit(int id){ var album = new Album{Price=12.0m}; return View(album); } 页面代码: @Html.TextBox Name",album.GenreId); return View(album); } 视图中的代码: @Html.TextBox("Title",Model.Title) 辅助方法在查找强类型模型对象之前
Html.BeginRouteForm(new { controller = "home", action = "process" })) {%> 2:
3: <label>用户名:</label><%=Html.TextBox ("username") %>
4:5: <label>密码:</label><%=Html.TextBox("password") %>
6: 4:5: <label>密码1:</label><%=Html.TextBox("a.password") %>
6:7: <label>用户名2:</label><%=Html.TextBox("b.username") %>
8:9: <label>密码2:</label><%=Html.TextBox("b.password") %>
10:11: <input type="submit" /></
HtmlHelper原先的功能不是很多,还好有很多扩展方法,我们能用它来生成一个Input控件,比如: <%= Html.TextBox("UserID") %> 我们使用上面的代码能生成一个没有值的单行文本框 ,我们还可以使用: <%= Html.TextBox("UserID", Model.User.UserID, new { @class="class" })%> 给这个文本框加值,加样式,与之对应的 随便说个例子,比如你要添加一个产品,可能它会有几十个属性,意味着你要写几十个Html.TextBox(属性值) (这里可以是其他控件),其实也没有什么,不过真的很不爽,可能我习惯了 Fluent NHibernate 的映射方式,我一直想着使用以下的方式来生成控件: <%= Html.TextBox<User>(u => u.UserID, Model.User.UserID, new { @class="class
"": ViewBag.Result.ToString(); 11: @Html.TextBox("op1") @:/ @Html.TextBox("op2") <input type="submit " value="=" /> @Html.TextBox("result",result) 12: } 三、异常处理策略 为了处理“除零异常”,我们专门对DivideByZeroException定义了相应的异常处理策略
input type="password">标签 Html.RadioButton() 输出<input type="radio">标签 Html.TextArea() 输出<textarea/>标签 Html.TextBox " method="post"> <fieldset> <legend>表单</legend>
@Html.TextBox ("userName", "", new { @placeholder = "请输入用户名" })
@Html.TextBox(
如果我们想提供一个input type=text 它的name为t1则以下代码: 1: <%=Html.TextBox("t1") %> 3.3表单元素绑定 如果我们想要让上文中的t1初始时就有一个值, 比如 “重典”那么我们可以按以下方式 1: <%=Html.TextBox("t1","重典") %> 如果数据是从数据库中读取,即得到数据是从Action中获取的,那么我们可以在Action中使用ViewData 传递 Action: 1: ViewData["name"]="重典"; View: 1: <%=Html.TextBox("t1",ViewData["name"]) %> 以上方法看似简单,其实ASP.NET 为我们提供了更为简便的绑定方式---只要保证ViewData的Key与Helper所生成元素的name保持一致就可以自动绑定: Action: 1: ViewData["t1"]="重典"; View: 1: <%=Html.TextBox
@Html.Label("lblName", "姓名")
3.Html.TextBox —— 单行的text @Html.TextBox("Title",Model.Title) 渲染一个type特性为text的input标签,用于接收用户自由形式的输入,等效的 checkbox" value="true" /> <input name="IsDiscounted" type="hidden" value="false" /> 辅助方法、模型和视图数据: 辅助方法如Html.TextBox ViewBag.Album = new Album {Price = 11}; 4 return View(); 5 } 在响应的视图中,可以使用下面这行代码来显示一个带有专辑价格的文本框: @Html.TextBox int id) 2 { 3 var album = new Album {Price = 12.0m}; 4 return View(album); 5 } 视图中的代码: @Html.TextBox 视图: @Html.TextBox("Title", Model.Title) 强类型的辅助方法 如果不适应使用字符串字面值从视图数据中提取值的话,也可以使用MVC提供的各种强类型辅助方法。
那么,我们为什么可以在页面中使用Html.TextBox()方法呢? link1">这是一个连接 RouteLink在用法几乎与ActionLink一致,这里就不再介绍,详情请参与MSDN; (2)TextBox与TextArea ①TextBox Html.TextBox ("input1") Html.TextBox("input2",Model.CategoryName,new{ @style = "width:300px;" }) Html.TextBox("input3 这是因为如果不改命名空间,我们要使用自定义的扩展方法需要在每个页面中引入Models(MyHtmlHelper所在的那个命名空间)这个命名空间,为了防止重复的命名空间引入操作(想想我们使用Html.TextBox
例如: @Html.TextBox("name",ViewBag.Name)。
举个栗子,以下代码产生一个type等于text的Input ,并且其id和name都等于CustomerName,其Value等于Northwind Traders: @Html.TextBox("CustomerName ","Northwind Traders"); 大多数内置的HTML helpers提供传入匿名类型为元素产生指定HTML属性的选项,对上述的@HTML.TextBox方法稍作修改,通过传入匿名类型设置输出元素的 style属性: @Html.TextBox("CustomerName","Northwind Traders", new { style="background-color:Blue;" })
@Html.TextBox("txt",null, new {@style="width:300;",onkeypress="return RegValidateIsDigit(event)" }) 可以看到在文本框中注册了
form-control" name="@(prefix)Title" value="@Model.Title" />
这些Helper放在@Html中,我们可以方便的使用:
@Html.TextBox("txtName")
OK,本节就讲到这里,更多的学习内容,敬请关注!@class = "navbar-form navbar-left", role = "search" })) {
这是因为如果不改命名空间,我们要使用自定义的扩展方法需要在每个页面中引入Models(MyHtmlHelper所在的那个命名空间)这个命名空间,为了防止重复的命名空间引入操作(想想我们使用Html.TextBox
88 <body> 89
", discriminatorTypes, new { @class = "form-control input-small", style = "width:120px" }) @Html.TextBox
Html.ActionLink("Create New", "Create")后面,添加以下内容: @using (Html.BeginForm()){
Title: @Html.TextBox Html.ActionLink("Create New", "Create") @using (Html.BeginForm()){
Title: @Html.TextBox FormMethod.Get)){
Genre: @Html.DropDownList("movieGenre", "All") Title: @Html.TextBox