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 6609747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:45:16+00:00 2026-05-25T19:45:16+00:00

I’m working on an MVC application and i’m trying to implement some validation. I’ve

  • 0

I’m working on an MVC application and i’m trying to implement some validation. I’ve strucuture the site to use EF for storage and a set of view models with automapper.

I want to add some validation which i’m sure would work if i added it to the View Models however i’m assuming it would be better to put validation in with the EF model so if in the future i create another interface the same validation would also apply.

First of is this the correct approach and second how do i get MVC to actually test the validation before saving the object. Currently it just skips my EF validation.

The address model is auto generated so i created this partial class to add the validation:

public partial class Address : IValidatableObject
{
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (!string.IsNullOrWhiteSpace(this.AddressLine1) &&
            !string.IsNullOrWhiteSpace(this.AddressLine2) &&
            !string.IsNullOrWhiteSpace(this.AddressLine3) &&
            !string.IsNullOrWhiteSpace(this.Town) &&
            !string.IsNullOrWhiteSpace(this.City) &&
            !string.IsNullOrWhiteSpace(this.County) &&
            !string.IsNullOrWhiteSpace(this.Postcode))
            yield return new ValidationResult("Address cannot be blank.");
    }
}

This is my view model class with the display names changed

public class AddressVM
{
    public int? ID { get; set; }

    [Display(Name = "Address line 1")]
    public string AddressLine1 { get; set; }

    [Display(Name = "Address line 2")]
    public string AddressLine2 { get; set; }

    [Display(Name = "Address line 3")]
    public string AddressLine3 { get; set; }

    [Display(Name = "Town")]
    public string Town { get; set; }

    [Display(Name = "City")]
    public string City { get; set; }

    [Display(Name = "County")]
    public string County { get; set; }

    [Display(Name = "Postcode")]
    public string PostCode { get; set; }
}

This is my controller

public ActionResult AddAddress(AddressVM vm)
{
    IncidentAddress theAddress = Mapper.Map<AddressVM, Address>(vm);
    if (ModelState.IsValid)
    {
        UOW.Addresses.Add(theAddress);
        UOW.Save();
    }
    return PartialView("AddressVM-edit", vm);
}
  • 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-25T19:45:16+00:00Added an answer on May 25, 2026 at 7:45 pm
    if (ModelState.IsValid)
    

    This will always be true for your object, as it will look for validity of your model, which is AddressVM (you receive that from view so this is your model) and does not have any validators. ModelState does not know that you have mapped this object to some other which implements validation. You need to run validation on your other object manually and add validation errors to ModelState.

    If you want to have this separated, you can implement IValidatableObject on AddressVM, and internally perform validation by creating a instance of Address, mapping it from AddressVM (this) and returning result of it’s Validate method. You also can expose the same constructed Address object as a property and use it to perform entity operation.

    Example of AddressVM:

    public class AddressVM : IValidatableObject     
    { 
        public int? ID { get; set; } 
    
        [Display(Name = "Address line 1")] 
        public string AddressLine1 { get; set; } 
    
        [Display(Name = "Address line 2")] 
        public string AddressLine2 { get; set; } 
    
        [Display(Name = "Address line 3")] 
        public string AddressLine3 { get; set; } 
    
        [Display(Name = "Town")] 
        public string Town { get; set; } 
    
        [Display(Name = "City")] 
        public string City { get; set; } 
    
        [Display(Name = "County")] 
        public string County { get; set; } 
    
        [Display(Name = "Postcode")] 
        public string PostCode { get; set; }
    
    
        //// I added this and interface in class definition:
    
        public IncidentAddress GetIncidentAddress()
        { 
            return Mapper.Map<AddressVM, Address>(this);   
        }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)      
        {
            return this.GetIncidentAddress().Validate(validationContext);
        }
    } 
    

    This way your logic stays in your business object, and your viewmodel uses it without having copy of it or some other dependency.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.