C# MVC中的验证

C# MVC中的验证,c#,asp.net-mvc,asp.net-mvc-3,razor,jquery-validate,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,Jquery Validate,我正在使用MVC3ASP.net和Razor,如何在客户端验证这些下拉框?如未选择任何值,则会将错误传递给请选择值的用户。 我已经将linq添加到sql类中,这些类会自动生成内容 @using (Html.BeginForm("Generated_PaySlip", "Home", FormMethod.Get)) { <fieldset> Months @Html.DropDownList("Month", "Select Date")

我正在使用MVC3ASP.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> 
}

您可以使用javascript获取所选值:

var dropdown = document.getElementById("YourDropdown");
var selectedValue = dropdown.options[dropdown.selectedIndex].value;
之后,您可以使用selectedValue检查用户选择的值是否有效

但是,我不建议这样做。最好使用Asp.NETMVC提供的工具。 首先,您应该创建一个模型来托管您需要显示下拉列表的atributes,在这个模型中,使用Data Anotations(初学者的简单指南可以在这里找到:)来验证其值。例如:

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;}
}
更新

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);
}

好的,但是我正在使用LINQtoSQL,它会影响它吗?喜欢模型吗?不,你们应该能够在你们的控制器逻辑中继续使用LINQ。我更新了我的答案来展示你们如何继续使用LINQ。
@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);
}