<h2>students</>
p>
@Html.ActionLink("Create New","Create")
>
@using (Html.beginForm("Index","student",FormMethod.Get))
{
>
Find by name:@Html.TextBox("SearchString",ViewBag.CurrentFilter as String)
input type="submit" value="Search"/>
>
}
a href="/Company/Create">Add New Workera>
1
@model SlarkInc.Models.Worker
2 @{
3 ViewBag.title = "Add New Worker";
4 }
5 >
6 @using (Html.beginForm())
7 {
8 @Html.AntiForgeryToken()
9 div class="form-horizontal"10 hr />
11 @Html.ValidationSumMary(true)
12 ="form-group"13 @Html.LabelFor(model => model.FirstName,new { @class = "control-label col-md-2" })
14 ="col-md-10"15 @Html.EditorFor(model => model.FirstName)
16 @Html.ValidationmessageFor(model => model.FirstName)
17 div18 19 20 @Html.LabelFor(model => model.LastName,128)">21 22 @Html.EditorFor(model => model.lastName)
23 @Html.ValidationmessageFor(model => model.lastName)
24 25 26 27 @Html.LabelFor(model => model.Sex,128)">28 29 @Html.EnumDropDownListFor(model => model.SeX)
30 @Html.ValidationmessageFor(model => model.SeX)
31 32 33 34 @Html.LabelFor(model => model.rating,128)">35 36 @Html.EditorFor(model => model.rating)
37 @Html.ValidationmessageFor(model => model.rating)
38 39 40 41 ="col-md-offset-2 col-md-10"42 @Html.Submit("Submit")
43 44 45 46 }
47 48 @Html.ActionLink("BACk to List","Index")
49 >
View的第1行代码如下所示,引入了Models文件夹里的Worker类。
@model SlarkInc.Models.Worker
为了更好的进行数据操作,Worker类做了改动,改动之后的代码如下:
1
using System.ComponentModel.DataAnnotations;
2 namespace SlarkInc.Models
3 {
4 public enum Sex
5 {
6 Male,Female
7 }
8 class Worker
9 {
int ID
{ get;
set; }
11 [Display(Name =
"last name")]
12 [DataType(DataType.Text)]
13 [required]
14 String LastName
{ 15 [Display(Name =
First Name16 [DataType(DataType.Text)]
17 [required]
String FirstName
{ 19 [required]
20 public Sex Sex
{ 21 double? Ra
Ting
{ 22 }
23 }
="control-label col-md-2" for="FirstName">First Name
label View中的第15行:
@Html.EditorFor(model => model.FirstName)
会根据Model中的第16行:
[DataType(DataType.Text)]
来决定用哪种input元素来编辑数据,既然是Text类型的,那就用type="text"的input,如下所示:
data-val="true" data-val-required="First Name 字段是必需的。" id="FirstName" name="FirstName" type="text"="" />
那上面的代码中 "data-val-required="First Name 字段是必需的。"" 这一段是哪来的呢?
这是EditorFor函数读取到Model中的第17行:
这一行表示这个数据是必填项,如果不填则会显示信息"First Name 字段是必需的。"。
在View中的第29行用到函数Html.EnumDropDownListFor,如下所示:
@Html.EnumDropDownListFor(model => model.SeX)
这个函数可以把Enum类型的数据在页面上以下拉菜单的形式显示出来供人编辑。
不过这个函数可不是那么容易用,首先Visual studio的版本必须是2013或者以上的,项目必须用的是MVC5,然后在菜单中选择工具->库程序包管理器-> 管理解决方案的NuGet程序包。如下所示选择联机,在左上角@L_480_6@mVC然后安装最新的MVC 5.2.2版。
@H_301_15@
更新好之后,这个函数就可以正常使用了。它会根据Sex这个Enum变量来生成下拉菜单。这个Enum的定义如下:
那么它生成的下拉菜单代码如下:
@H_
618_409@
SELEct
data-val="Sex 字段是必需的。"="Sex"="Sex"option SELEcted="SELEcted"="0">@H_168_25@maleoptionvalue="1">FemaleSELEct View的第16行代码:
@Html.ValidationmessageFor(model => model.FirstName)
其中ValidationmessageFor函数用来验证数据的有效性。它根据在Model中这个属性的类型来验证输入的值是否符合要求。比如rating这个属性是Double类型的,那么在输入数据时,如果数据不是数字则会有相应提示,并且不能提交。
View的第6、7、46行是如下所示的不带参数的Form函数结构:
@using (Html.beginForm()){}
这样的结构如果不带任何参数,则Form会以Post方法提交给本页面对应的Controller和Action,因此其生成的HTML代码就是如下形式:
<form action=/Company/Create" method=post"></form>
在View中使用了Bootstrap的横向表单布局其结构如下:
>
>
=""></>
显示出来的效果如下所示:
@H_301_15@
每一行对应一个属性,左边是属性名,右边是属性对应的编辑框。属性名的col-md-2表示其占Form总宽度的2/12,col-md-10表示其占Form总宽度的10/12。这用到了Bootstrop的栅格系统。栅格系统详细介绍请点这里。
View第8行@Html.AntiForgeryToken()函数的作用是抵御网页跨站请求伪造漏洞(CSRF Cross-site request forgery)。这个漏洞可以盗用登录用户身份发送恶意请求。比如一个用户登录了网上银行,然后访问攻击者的网站,网站就会通过登录用户发出请求来获取银行信息。
View第11行使用Html.ValidationSumMary(true)。表示只@L_37_114@model级的验证错误信息。其具体用法会在后面章节中详细介绍。
上面这几段对Create.cshtml文件中具有代表性的技术知识点做了详细说明,其他行不再赘述,有问题请留言。
添加数据Action
从上面代码可以看出,Create.cshtml页面会把数据提交给当前页面对应的Controller和Action,因此我们就在CompanyController下写处理提交的数据的Action。代码如下:
1
[httpPost]
2 [ValidateAntiForgeryToken]
3 public ActionResult Create([Bind(Include = FirstName,LastName,Sex,rating")] Worker worker)
4 {
5 try
6 {
7 if (ModelState.IsValid)
8 {
9 db.Workers.Add(worker);
10 db.SaveChanges();
11 return RedirectToAction(Index");
12 }
13 }
14 catch (DataException /* dex */)
15 {
16 ModelState.AddModelError("unableToSave",Unable to save changes.Try again,and if the problem persists see your system administrator.17 }
18 return View(worker);
19 }
本图文内容来源于网友网络收集整理提供,作为学习参
考使用,版权属于原作者。
@H_262_745@ 编程之家官方2群(满)