Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:11:20+00:00 2026-05-30T00:11:20+00:00

I have an employee class in model for ASP.NET MVC3. There is a field

  • 0

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

  1. ASP.NET MVC3: ValidationType ModelClientValidationRule

  2. How does DataAnnotations really work in MVC?

  3. ASP.NET MVC 3 client-side validation with parameters

  4. http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

  5. Add Sorting and Searching in Contact Management ASP.NET MVC Application

  6. http://trainingkit.webcamps.ms/AspNetMvc.htm

  7. ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present

  8. http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1

  9. What is the better ASP.NET MVC 3.0 Custom Validation approach

  10. http://dotnetslackers.com/articles/aspnet/Validating-Data-in-ASP-NET-MVC-Applications.aspx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:11:22+00:00Added an answer on May 30, 2026 at 12:11 am

    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<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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =
I'm using ASP.Net MVC3, and Entity Framework 4. Employee has an OfficeLocation, which is
I have a model class Employee_Type(models.Model): def __unicode__(self): return self.name name = models.CharField(max_length=200, verbose_name=employee
When building a view model in asp.net's MVC3, where should the code go to
Let's say I have the following: class Employee(models.Model): firstName = models.CharField(max_length = 30) lastName
(just learning MVC) I have created a model class: public class Employee { public
I have a ASP.Net MVC form that is bound to the following view model.
I have an employee entity in my EF model. I then added a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.