简体   繁体   English

在MVC中验证

[英]Validation in MVC

I am using MVC 3 asp.net and Razor, how to validate these drop down boxes on client side? 我正在使用MVC 3 asp.net和Razor,如何在客户端验证这些下拉框? Like if no value is selected that pass error to user that please select value. 就像没有选择任何值将错误传递给用户一样,请选择值。 I have added linq to sql classes which automatically generate things 我已将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> 
}

Controller: 控制器:

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

You can use javascript to get the selected value: 您可以使用javascript获取所选值:

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

After this, you can use the selectedValue to check if the value the user selected is valid. 此后,您可以使用selectedValue来检查用户选择的值是否有效。

But, I do not recommend doing this way. 但是,我不建议这样做。 It's better to use the tools that the Asp.net MVC provides. 最好使用Asp.net MVC提供的工具。 First, you should create a model to host the atributes you need to display a dropdown for and, in this model, use the Data Anotations (a simple guide for beginners can be found here: http://msdn.microsoft.com/en-us/library/ee256141(v=vs.100).aspx ) to validate its values. 首先,您应该创建一个模型来承载显示下拉列表所需的属性,并且在该模型中,使用数据注释(可以在此处找到针对初学者的简单指南: http : //msdn.microsoft.com/en -us / library / ee256141(v = vs.100).aspx )以验证其值。 For instance: 例如:

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

In your View, you would have: 在您的视图中,您将具有:

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

And, finnaly, in your controller, your action would look like this: 最后,在您的控制器中,您的操作将如下所示:

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

UPDATE 更新

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