簡體   English   中英

在MVC中驗證

[英]Validation in MVC

我正在使用MVC 3 asp.net和Razor,如何在客戶端驗證這些下拉框? 就像沒有選擇任何值將錯誤傳遞給用戶一樣,請選擇值。 我已將linq添加到自動生成內容的sql類中

@using (Html.BeginForm("Generated_PaySlip", "Home", FormMethod.Get))
{
    <fieldset>
         Months
         @Html.DropDownList("Month", "Select Date")
         &nbsp &nbsp

         Employee Name
         @Html.DropDownList("EmplID", "Select Name")
         &nbsp &nbsp
         <input type="submit" value="Submit" />
   </fieldset> 
}

控制器:

public ActionResult InfoFor_PaySlip() 
{
    var dates = (from ps in DataContext.MonthlyRecords select new {ps.Month }).Distinct();
    ViewData["Month"] = new SelectList(dates, "Month", "Month");

    var names = (from n in DataContext.HrEmployees select new { n.EmplID, n.EmplName }).Distinct();
    ViewData["EmplID"] = new SelectList(names, "EmplID", "EmplName");

    return View();
 }

您可以使用javascript獲取所選值:

var dropdown = document.getElementById("YourDropdown");
var selectedValue = dropdown.options[dropdown.selectedIndex].value;

此后,您可以使用selectedValue來檢查用戶選擇的值是否有效。

但是,我不建議這樣做。 最好使用Asp.net MVC提供的工具。 首先,您應該創建一個模型來承載顯示下拉列表所需的屬性,並且在該模型中,使用數據注釋(可以在此處找到針對初學者的簡單指南: http : //msdn.microsoft.com/en -us / library / ee256141(v = vs.100).aspx )以驗證其值。 例如:

public class YouModel{

  [Display(Name = "Month")]
  [Required(ErrorMessage = "This field is required")]
  public String SelectedMonth {get; set;}
  public List<SelectListItem> Months {get; set;}

  [Display(Name = "Employee Name")]
  [Required(ErrorMessage = "This field is required")]
  public int SelectedEmployee {get; set;}
  public List<SelectListItem> EmployeeList {get; set;}
}

在您的視圖中,您將具有:

@using location.of.YouModel

@using (Html.BeginForm("Generated_PaySlip", "Home", FormMethod.Get))
{
  <fieldset>
      @Html.LabelFor(m => m.SelectedMonth)
      @Html.DropDownListFor(m => m.SelectedMonth, Model.Months)
      @Html.ValidationMessageFor(m => m.SelectedMonth)

      @Html.LabelFor(m => m.SelectedEmployee)
      @Html.DropDownListFor(m => m.SelectedEmployee, Model.EmployeeList)
      @Html.ValidationMessageFor(m => m.SelectedEmployee)

      <input type="submit" value="Submit" />
  </fieldset>
}

最后,在您的控制器中,您的操作將如下所示:

public ActionResult Generated_PaySlip(YourModel model){

// The ModelState.isValid will verify if your model is validated based on the data Anotations you used in the model. If not, will return false and you just have to return the model to the view you want and the framework will do the job of displaying the validation messages.
  if (ModelState.isValid){
    // do something
  }
  return View(model);
}

更新

public ActionResult InfoFor_PaySlip() 
{
    YourModel model = new YourModel();

    var dates = (from ps in DataContext.MonthlyRecords select new {ps.Month }).Distinct();
    model.Months = new SelectList(dates, "Month", "Month");

    var names = (from n in DataContext.HrEmployees select new { n.EmplID, n.EmplName }).Distinct();
    model.EmployeeList = new SelectList(names, "EmplID", "EmplName");

    return View(model);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM