Asp.net mvc中的属性验证

Asp.net mvc中的属性验证,asp.net,asp.net-mvc,asp.net-mvc-4,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在尝试验证用户在表单中输入的字段。我试过了,但它根本没有验证字段。即使我没有输入任何记录,它也不会给出错误消息。我还需要添加脚本或库之类的内容吗? 要添加哪个库 视图模型(类): 以下是观点: <form action="@Url.Action("Index","SignUp")" class="form-horizontal" method="post"> Full Name @Html.TextBoxFor(x => x.Full_Name, new { @c

我正在尝试验证用户在表单中输入的字段。我试过了,但它根本没有验证字段。即使我没有输入任何记录,它也不会给出错误消息。我还需要添加脚本或库之类的内容吗? 要添加哪个库

视图模型(类):

以下是观点:

    <form action="@Url.Action("Index","SignUp")" class="form-horizontal" method="post">

Full Name @Html.TextBoxFor(x => x.Full_Name, new { @class = "form-control" })
@Html.ValidationMessageFor(x=>x.Full_Name)
<br />
Email @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Email)
<br />
Password  @Html.TextBoxFor(x => x.pass, new { @class = "form-control", type = "password" })
@Html.ValidationMessageFor(x => x.pass)
<br />
Date of Birth @Html.TextBoxFor(x => x.Date_of_Birth, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Date_of_Birth)
<br />
Address @Html.TextBoxFor(x => x.Home_Address, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Home_Address)
<br />
Mobile Number-1 @Html.TextBoxFor(x => x.Mobile_Number1, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Mobile_Number1)
<br />
CNIC @Html.TextBoxFor(x => x.CNIC, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Full_Name)
<br />
Country @Html.TextBoxFor(x => x.Country, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Country)
<br />
Provience @Html.TextBoxFor(x => x.Provience, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.Provience)
<br />
City @Html.TextBoxFor(x => x.City, new { @class = "form-control" })
@Html.ValidationMessageFor(x => x.City)
<br />

@{
    List<SelectListItem> listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem
    {
        Text = "Student",
        Value = "Student"
    });
    listItems.Add(new SelectListItem
    {
        Text = "Trainer",
        Value = "Trainer",
        Selected = true
    });

}

@Html.DropDownListFor(model => model.User_Role, listItems, "-- Select Role --")
@Html.ValidationMessageFor(x => x.User_Role)
<br />
<br />
@{
    List<SelectListItem> listItems2 = new List<SelectListItem>();
    listItems2.Add(new SelectListItem
    {
        Text = "Male",
        Value = "Male"
    });
    listItems2.Add(new SelectListItem
    {
        Text = "Female",
        Value = "Female",
        Selected = true
    });

}

@Html.DropDownListFor(model => model.Gender, listItems2, "-- Select Gender --")
@Html.ValidationMessageFor(x => x.Gender)
<br />
<br />
<button type="submit" class="btn btn-primary"> Sign Up </button>

全名@Html.TextBoxFor(x=>x.Full_Name,新{@class=“form control”})
@Html.ValidationMessageFor(x=>x.Full_Name)

Email@Html.TextBoxFor(x=>x.Email,新的{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Email)
Password@Html.TextBoxFor(x=>x.pass,新的{@class=“form control”,type=“Password”}) @Html.ValidationMessageFor(x=>x.pass)
出生日期@Html.TextBoxFor(x=>x.Date\u of\u出生,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Date\u of\u出生)
地址@Html.TextBoxFor(x=>x.Home_地址,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Home\u地址)
Mobile Number-1@Html.TextBoxFor(x=>x.Mobile_Number1,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Mobile_Number1)
CNIC@Html.TextBoxFor(x=>x.CNIC,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Full_Name)
Country@Html.TextBoxFor(x=>x.Country,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Country)
provence@Html.TextBoxFor(x=>x.provence,新的{@class=“form control”}) @Html.ValidationMessageFor(x=>x.Provence)
City@Html.TextBoxFor(x=>x.City,新{@class=“form control”}) @Html.ValidationMessageFor(x=>x.City)
@{ List listItems=新列表(); 添加(新建SelectListItem) { Text=“学生”, Value=“学生” }); 添加(新建SelectListItem) { Text=“培训师”, Value=“培训师”, 所选=真 }); } @Html.DropDownListFor(model=>model.User\u角色,列表项,“--Select Role--”) @Html.ValidationMessageFor(x=>x.User\u角色)

@{ List listItems2=新列表(); listItems2.添加(新建SelectListItem { Text=“男”, Value=“男性” }); listItems2.添加(新建SelectListItem { Text=“女性”, Value=“女性”, 所选=真 }); } @Html.DropDownListFor(model=>model.Gender,listItems2,“--Select-Gender-->”) @Html.ValidationMessageFor(x=>x.Gender)

注册

HttpPost操作中的当前代码总是使用
RedirectToAction
方法返回RedirectResult。如果希望在提交表单时查看模型验证框架生成的验证错误,则需要返回到同一视图

您还可以使用
ModelState.IsValid
属性检查提交的表单数据是否通过了验证。如果有,您可以继续执行代码,如保存到db和重定向

[HttpPost] 
public ActionResult Index(RegisterationLoginViewModel svm)
{ 
   if(ModelState.IsValid)
   {
     //save
     var result = db.Register(svm);
     return RedirectToAction("ThankYou_View");
   }
   //Model validation failed. Let's return to same view.
   // to do : Make sure to reload any stuff you need in view (Ex: ViewBag etc)
   return View(svm);
}

您的httppost操作方法是什么样子的?[httppost]公共操作结果索引(RegisterationLoginViewModel svm){if(svm==null){RedirectToAction(“null\u SignUp\u View”);}字符串RegisterStudent\u Response=db.Register(svm);if(RegisterStudent_Response==“1”){RedirectToAction(“Thankyu_View”)}返回RedirectToAction(“Thankyu_View”)}
[HttpPost] 
public ActionResult Index(RegisterationLoginViewModel svm)
{ 
   if(ModelState.IsValid)
   {
     //save
     var result = db.Register(svm);
     return RedirectToAction("ThankYou_View");
   }
   //Model validation failed. Let's return to same view.
   // to do : Make sure to reload any stuff you need in view (Ex: ViewBag etc)
   return View(svm);
}