2014-02-13 52 views
0

我正在使用MVC 3 asp.net和Razor,如何验证客户端的这些下拉框?如果没有选择将值传递给用户的值,请选择值。 我已经添加LINQ to SQL类自动生成的东西验证MVC

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

回答

0

您可以使用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> 
} 

而且,finnaly,在你的控制器,你的动作应该是这样的:

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

确定但我使用LINQ TO SQL,它会影响它吗?像模型? – PreciseTech

+0

不,你应该可以继续在你的控制器逻辑中使用LINQ。 –

+0

我更新了我的答案,以显示如何继续使用LINQ。 –