2017-08-24 97 views
-1

我想驗證用戶在表單中輸入的字段。我試過這個,但它根本不驗證字段。即使我沒有輸入記錄,它也不會給出錯誤信息。還有什麼我需要添加像腳本或庫? 哪個庫要添加?Asp.net mvc中的屬性驗證

視圖模型(類):

public class RegisterationLoginViewModel 
{ 
    [Required] 
    public string Email { get; set; } 
    [Required] 
    public string pass { get; set; } 
    [Required] 
    public string Full_Name { get; set; } 
    [Required] 
    public string Mobile_Number1 { get; set; } 
    [Required] 
    public DateTime Date_of_Birth { get; set; } 
    [Required] 
    public string Gender { get; set; } 
    [Required] 
    public string CNIC { get; set; } 
    public DateTime Created_On { get; set; } 
    [Required] 
    public string City { get; set; } 
    [Required] 
    public string Provience { get; set; } 
    [Required] 
    public string Country { get; set; } 
    [Required] 
    public string Home_Address { get; set; } 
    [Required] 
    public string User_Role { get; set; } 
    public int Approved { get; set; } 

} 

下面是查看:

<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> 

+0

你的httppost動作方法是怎樣的? – Shyju

+0

[HttpPost] 公共的ActionResult指數(RegisterationLoginViewModel SVM) { 如果(SVM == NULL){ RedirectToAction( 「Null_SignUp_View」); } string RegisterStudent_Response = db.Register(svm); if(RegisterStudent_Response ==「1」) { RedirectToAction(「ThankYou_View」); } return RedirectToAction(「ThankYou_View」); } –

回答

0

您當前的代碼在你的HttpPost動作總是返回使用RedirectToAction方法RedirectResult。如果您希望在提交表單時看到由模型驗證框架生成的驗證錯誤,則需要返回到相同的視圖。

您也可以使用ModelState.IsValid屬性來檢查提交的表單數據是否通過了驗證。如果是這樣,你可以繼續執行你的代碼,如保存到數據庫和重定向。

[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); 
} 
相關問題