在mvc 3中验证控件 [英] validating controls in mvc 3
本文介绍了在mvc 3中验证控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MVC 3 asp.net和Razor,如何验证这些下拉框?在客户端。如果没有选择将错误传递给用户的值,请选择值。
我已将linq添加到自动生成内容的sql类中
查看:
i am using MVC 3 asp.net and Razor, how to validate these drop down boxes ? on client side. 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
VIEW:
@using (Html.BeginForm("Generated_PaySlip", "Home", FormMethod.Get))
{
<fieldset>
Months
@Html.DropDownList("Month", "Select Date")
Employee Name
@Html.DropDownList("EmplID", "Select Name")
<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();
}
推荐答案
您创建了下拉列表,您可以使用DropdownListFor并且您可以为MVC的属性编写ValidationMessage
You created dropdown you can use DropdownListFor And you can write ValidationMessagefor properties of MVC
参考
mvc3-how-do-i-validate-a-drop-down-list [ ^ ]
< a href =http://stackoverflow.com/questions/11672880/how-to-client-side-validate-the-dropdownlist-box-in-razor-mvc3> how-to-client-side-validate-the -dropdownlist-box-in-razor-mvc3 [ ^ ]
这篇关于在mvc 3中验证控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文