Your model can implement the IValidatableObject (inside System.ComponentModel. DataAnnotations namespace ).
Up vote 1 down vote favorite share g+ share fb share tw.
I have an employee class in model for ASP. NET MVC3. There is a field named “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”. (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"; } Create @using (Html.BeginForm()) { EmpName :~: @Html. EditorFor(model => model. EmpName) 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 How does DataAnnotations really work in MVC?
ASP. NET MVC 3 client-side validation with parameters http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html Add Sorting and Searching in Contact Management ASP. NET MVC Application http://trainingkit.webcamps.
Ms/AspNetMvc. Htm ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present 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 Vlidation approach http://dotnetslackers.com/articles/aspnet/Validating-Data-in-ASP-NET-MVC-Applications.aspx c# asp.net asp.net-mvc asp.net-mvc-3 mvc link|improve this question edited Feb 19 at 14:38 asked Feb 19 at 5:37Lijo68318 73% accept rate.
– CjCoax Feb 19 at 6:10 I am just learning MVC. I used Employee object that I created in controller page. A similar approach I have posted in another question.
Please refer stackoverflow.com/questions/9340093/… . Also, in real sceanrio, I will be getting database values using a WCF calls. I am planning to call WCF services directly from controller.
Is that good approach? – Lijo Feb 19 at 6:12 Is the approach mentioned in the following suitable here? Stackoverflow.com/questions/9349766/….
– Lijo Feb 19 at 14:37.
Your model can implement the IValidatableObject(inside System.ComponentModel. DataAnnotations namespace) Your model should be something like below: public class Employee : IValidatableObject { public string EmployeeName; public IEnumerable Validate(ValidationContext validationContext) { char secondNameChar = EmployeeName1; char thirdNameChar = EmployeeName2; 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. 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.
I have upvoted your suggestion. Howver, is the approach mentioned in the following suitable here? Stackoverflow.com/questions/9349766/….
– Lijo Feb 19 at 14:37 You may use self validation (implementing IValidatableObject) where ever you have your entities. You can use this approach on your entities within a WCF service as well(to validate your Data Contracts). – CjCoax Feb 19 at 23:24.
You can add server side errors using ModelState.AddModelError. When the ModelState contains errors it will result in ModelState. IsValid being 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: EmpName :~: @Html. TextBoxFor(model => model. EmpName) @Html.
ValidationMessageFor(model => model. EmpName).
I have upvoted your suggestion. Howver, is the approach mentioned in the following suitable here? Stackoverflow.com/questions/9349766/….
– Lijo Feb 19 at 14:37.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.