[英]ASP.NET MVC3: Validation Summary for Create Action (Server Side Validation)
I have an employee class in model for ASP.NET MVC3. 我在ASP.NET MVC3模型中有一个员工类。 There is a field named “EmpName”. 有一个名为“ EmpName”的字段。 There is a create action for creating employee records. 有一个用于创建员工记录的创建动作。 There should be a server side validation that - the second letter of the name should be “E” and third letter of the name should be “F”. 应该在服务器端进行验证-名称的第二个字母应为“ E”,名称的第三个字母应为“ F”。 (There is no client side validation). (没有客户端验证)。 If the validation is failed, the message should be displayed in the create view as a validation summary. 如果验证失败,则该消息应作为验证摘要显示在创建视图中。 How do we do this? 我们如何做到这一点?
Note: Validation errors for these two validations is expected to come as two error result (two different line). 注意:这两个验证的验证错误预计将作为两个错误结果(两个不同的行)出现。
Note: I am not using Entity Framework 注意:我没有使用实体框架
Following is the view code. 以下是查看代码。
@model MyApp.Employee
@{ ViewBag.Title = "Create"; }
<h2>Create</h2>
@using (Html.BeginForm())
{
<div > EmpName :~: @Html.EditorFor(model => model.EmpName) </div>
CONTROLLER 控制器
// GET:
public ActionResult Create()
{
return View();
}
// POST:
[HttpPost]
public ActionResult Create(Employee emp)
{
if (ModelState.IsValid)
{
//Save the employee in DB first and then redirectToAction.
return RedirectToAction("Index");
}
}
READING: 读:
ASP.NET MVC3: ValidationType ModelClientValidationRule ASP.NET MVC3:ValidationType ModelClientValidationRule
How does DataAnnotations really work in MVC? DataAnnotations在MVC中如何真正起作用?
ASP.NET MVC 3 client-side validation with parameters 带参数的ASP.NET MVC 3客户端验证
http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html
Add Sorting and Searching in Contact Management ASP.NET MVC Application 在联系人管理ASP.NET MVC应用程序中添加排序和搜索
http://trainingkit.webcamps.ms/AspNetMvc.htm http://trainingkit.webcamps.ms/AspNetMvc.htm
ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present 在没有错误的情况下显示带有自定义CSS的ValidationSummary和ValidationMessageFor
http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1 http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1
What is the better ASP.NET MVC 3.0 Custom Validation approach 什么是更好的ASP.NET MVC 3.0自定义验证方法
http://dotnetslackers.com/articles/aspnet/Validating-Data-in-ASP-NET-MVC-Applications.aspx http://dotnetslackers.com/articles/aspnet/Validating-Data-in-ASP-NET-MVC-Applications.aspx
You can add server side errors using ModelState.AddModelError . 您可以使用ModelState.AddModelError添加服务器端错误。 When the ModelState
contains errors it will result in ModelState.IsValid
being false. 当ModelState
包含错误时,将导致ModelState.IsValid
为false。
[HttpPost]
public ActionResult Create(Employee emp)
{
if (YourServerValidation(emp) == false)
{
ModelState.AddModelError("EmpName", "Invalid value");
}
if (ModelState.IsValid)
{
//Save the employee in DB first and then redirectToAction.
return RedirectToAction("Index");
}
else
{
return View(emp);
}
Your view should be updated as follows: 您的视图应更新如下:
<div>
EmpName :~: @Html.TextBoxFor(model => model.EmpName)
@Html.ValidationMessageFor(model => model.EmpName)
</div>
Your model can implement the IValidatableObject
(inside System.ComponentModel.DataAnnotations namespace
) 您的模型可以实现IValidatableObject
(在System.ComponentModel.DataAnnotations namespace
内部)
Your model should be something like below: 您的模型应如下所示:
public class Employee : IValidatableObject
{
public string EmployeeName;
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
char secondNameChar = EmployeeName[1];
char thirdNameChar = EmployeeName[2];
if (secondNameChar.ToString().ToLower() != "e")
yield return
new ValidationResult("Second char of name should be 'E'",
new[] {"EmployeeName"});
if (thirdNameChar.ToString().ToLower() != "f")
yield return
new ValidationResult("Third char of name should be 'F'",
new[] {"EmployeeName"});
}
}
Please note that add other properties of the employee to this class, and do your validation inside Validate
method. 请注意,将雇员的其他属性添加到此类中,然后在Validate
方法中进行Validate
。
And now on the controller if you call ModelState.IsValid
on an invalid object, this fails and return two errors and show them both to the user. 现在在控制器上,如果您在无效对象上调用ModelState.IsValid
,则此操作将失败并返回两个错误,并向用户显示它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.