Html.text(转载)
2020-11-16 03:11
标签:style blog http 使用 数据 width
2、Html.ValidationSummary:用来显示ModelState字典中所有验证错误的无序列表,使用布尔值类型参数(true)来告知辅助方法排除属性级别的错误,只显示ModelState中与模型本身有关的错误,而不显示与具体模型属性相关的错误。
3、示例:
ModelState.AddModelError("",
"This is all wrong!");
ModelState.AddModelError("Title", "what a
terrible
name!");
在控制器某个操作中添加以上代码用来渲染编辑视图,
第一个错误是模型级别的,因为代码中没有提供关联错误与特定属性的键,
第二个错误是与属性相关联的错误,因此在视图验证摘要区不会显示这个错误
添加输入元素
1、Html.TextBox(和Html.TextArea)
调用形式如下:
@Html.TextBox("Title",Model.Title);
@Html.TextArea("text","Hello");
重载版本:@Html.TextArea("text","hello
world",10,80,null);
渲染为
1、通过辅助方法赋予与Viewbag中的值相同的名称显示文本框的价格
如:在控制器中:ViewBag.Price=10.0;
在视图中:@Html.TextBox("Price");
如:在控制器中:ViewBag.Album=new
Album{ Price=11};
在视图中:
@Html.TextBox("Album.Price");
2、辅助方法依靠强类型的视图数据也能很好的工作
如:在控制器中:var
album=new Album{
Price=12.0};
在视图(强类型)中:@Html.TextBox("Price");
3、向表单辅助方法提供显示的值
在控制器中:var
album=storeDB.Albums.Single(a=>a.AlbumId==id);
ViewBag.Genres=new
SelectList(storeDB.Genres.OrderBy(g=>g.Name),"GenerId","Name",album.GenreId);
在视图(强类型)中:
@Html.TextBox("Title",Model.Title);
强类型辅助方法
该方法需要为其传递一个lambda表达式来指定要渲染的模型属性,且表达式的模型必须和为视图指定的模型类型一致。
例:@Html.LabelFor(m=>m.GenreId);
@Html.DropDownListFor(m=>m.GenreId,ViewBag.Genres
as
SelectList)
@Html.ValidationMessageFor(m=>m.Title)
这些强类型的辅助方法名都以For作为其后缀,使用lambda表达式可是轻松实现代码的重构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
ActionResult HtmlText()
{
ViewData[ "Name" ] = "jade" ;
var
user = new
User() { Name = "jim" , Age =32 };
ViewData.Model = user;
List new
List
items.Add( new
SelectListItem { Text = "Kirin" , Value = "29"
});
items.Add( new
SelectListItem { Text = "Jade" , Value = "28" , Selected = true
});
items.Add( new
SelectListItem { Text = "Yao" , Value = "24"
});
this .ViewData[ "list" ] = items;
List new
List
items2.Add( new
SelectListItem { Text = "Kirin" , Value = "29"
});
items2.Add( new
SelectListItem { Text = "Jade" , Value = "28"
});
items2.Add( new
SelectListItem { Text = "Yao" , Value = "33"
});
this .ViewData[ "list2" ] = items2;
this .ViewData[ "selected" ] = 33;
ViewBag.Album = new
Album { Price = 11 };
return
View();
}
|
ASP.NET MVC提供了多种表单元素的Helper。其中包括:TextBox(类似input type=text,下面类似)、TextArea、DropDownList(select)、CheckBoxHidden、ListBox、 Password、RadionButton。注意:因为一般情况下是不会绑定数据的所以ASP.NET MVC并未提供此Helper(曾经提供过在preview2之前)。如果我们想提供一个input type=text 它的name为t1则以下代码:1:
3.3表单元素绑定
如果我们想要让上文中的t1初始时就有一个值,比如 “重典”那么我们可以按以下方式1: 如果数据是从数据库中读取,即得到数据是从Action中获取的,那么我们可以在Action中使用ViewData传递Action:1: ViewData["name"]="重典";View:1: 以上方法看似简单,其实ASP.NET MVC为我们提供了更为简便的绑定方式---只要保证ViewData的Key与Helper所生成元素的name保持一致就可以自动绑 定:Action:1: ViewData["t1"]="重典";View:1: 这样就可以自动绑定了
3.4列表数据显示与绑定
像TextBox这种值单一的数据比较容易,但是存在的数据比较多的 DropDownList或ListBox应该怎么绑定数据及初始化值呢,我们来看看下面的例子:Action:1: ViewData["sel1"] = new SelectList( 2:new[] {1, 2, 3} /*列表内容可以是数组*/ 3: , 3 /*默认值,可以是从数据库读出的*/ 4: );View:1: 这样就可以将列表内容、默认值、以及表单元素三者绑定在一起了。而我们的列表 内容并不是任何情况下都是数组的,大多情况下还是Key-Value对居多。
我们可以使用以下方式:
1: List
2: {
3:new SelectListItem {Text = "重典", Value = "1"},
4:new SelectListItem {Text = "邹健", Value = "2"},
5: };
6: ViewData["sel1"] = new SelectList(
7: list /*列表内容可以是数组*/
8: , "2"/*默认值,可以是从数据库读出的*/
9:
);
10.TextBox , Hidden
a.CategoryName, new { @style = "width:300px;" })%>
生成结果:
11.TextArea
a.CategoryName, 3, 3, null)%>
生成结果:
?
12.CheckBox
a.IsVaild, new { @class = "checkBox" })%>
生成结果:
?
13.ListBox
?
a.CategoryName, (SelectList)ViewData["Categories"])%>?
生成结果:
?
?
?
?
?
?
?
?
14.DropDownList
a.CategoryName, (SelectList)ViewData["Categories"], "--Select One--", new { @class = "dropdownlist" })%>
生成结果:
?
?
?
?
?
?
?
?
Html.text(转载),搜素材,soscw.com
Html.text(转载)
标签:style blog http 使用 数据 width
原文地址:http://www.cnblogs.com/yhf286/p/3699631.html