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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:21:18+00:00 2026-06-17T17:21:18+00:00

Environment: I am using MVC4, Razor 2 and FluentValidation.MVC4 (3.4.6.0). Scenario: I have a

  • 0

Environment:
I am using MVC4, Razor 2 and FluentValidation.MVC4 (3.4.6.0).

Scenario:
I have a complex view model for a particular page which also has a child view model on it as follows:

public class ProfileViewModel
{
    public string FirstName {get; set;}
    public PhoneNumberViewModel Primary {get; set;}
    // ... other stuff ... //
}

public class PhoneNumberViewModel
{
    public string AreaCode { get; set; }
    public string Exchange { get; set; }
    public string Suffix { get; set; }
    public string Extension { get; set; }
}

This profile can be edited and POSTed back for updating. I have created Fluent Validators for both as follows:

public class ProfileViewModelValidator : AbstractValidator<ProfileViewModel>
{
    public ProfileViewModelValidator()
    {
        RuleFor(m => m.FirstName).NotEmpty().WithMessage("Please enter a First Name,");
        RuleFor(m => m.Primary).SetValidator(new PhoneNumberViewModelValidator()).WithMessage("Hello StackOverflow!");
        // ... other validation ... //
    }
}

public class PhoneNumberViewModelValidator : AbstractValidator<PhoneNumberViewModel>
{
    public PhoneNumberViewModelValidator()
    {
        RuleFor(m => m.AreaCode).NotEmpty();
    }     
}

And then, of course, I have views to display everything.

Profile View Snippet:

...
@Html.TextBoxFor(m => m.FirstName)
@Html.EditorFor(m => m.PrimaryPhoneNumber)
...

Phone Number Editor Template Snippet:

...
@Html.ValidationLabelFor(m => m, "Primary Phone:")
@Html.TextBoxFor(m => m.AreaCode)
@Html.TextBoxFor(m => m.Exchange)
@Html.TextBoxFor(m => m.Suffix)
@Html.TextBoxFor(m => m.Extension) 
@Html.ValidationMessageFor(m => m)
...

If it’s relevant, I have things set up so that it automatically wires up validators with the various objects. I don’t actually even need the .SetValidator() line above… everything is validated anyway because of wire up.

Objective:
If I don’t enter a first name, I get the error message provided above in the area created by ValidationMessageFor. However, if any of the elements of the child PhoneNumberViewModel fail validation, I get nothing. The Text Boxes highlight red, which is fantastic, but I do not get the message I supplied in the .WithMessage(), indicating that my child property is invalid.

Currently I am achieving it by extra work in my controller… that looks for errors on the child objects and then adds errors to the parent object. This approach smells really, really bad. It’s putting validation-related concerns in the controller, and I just don’t want them there. Not to mention it also ends up having a lot of indexing by strings to dig into the ModelState, and it’s just… gross.

Is there a simple way to define a validation rule for the ProfileViewModelValidator that will add errors for the ProfileViewModel if the child fails to validate? And/or should it be working, but am I doing something wrong? I’ve searched and searched, but I cannot find a satisfactory solution.

Thanks for your time!

  • 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-06-17T17:21:20+00:00Added an answer on June 17, 2026 at 5:21 pm

    I have discovered a different solution from the smelly one I included in my question. It is also less than ideal, but I think I prefer it to the solution provided above. Perhaps others will agree, so it may be useful until a cleaner answer comes along.

    I added a property to the PhoneNumberViewModel that returns the entire phone number as a formatted string:

    public string FullNumber
    {
       get { return string.Format("{0}{1}{2}{3}", AreaCode, Exchange, Suffix, Extension); }
    }
    

    You can then apply validation rules to this property in addition to your rules for each of the component properties. If you only apply the .WithMessage() to the FullNumber property, you will end up getting both the validation message you expect (and only once) as well as the textbox highlighting for each individual box if there is a failure.

    The main downside to this approach is that you end up duplicating your validation rules. You validate each component individually, and then have to validate the combined property also which will basically be a combination of the validation you have already done. You’re doubling the code, and doubling the amount of processing. You’re also doubling the amount of places something can go wrong.

    In my case I am using regular expressions, so it isn’t too bad… and I much prefer it to embedding error propagation code in the controller. It’s still not ideal however – hopefully a better solution will come along.

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

Sidebar

Related Questions

I've got really used to developing views in MVC4 using Razor. But I have
we have a mixed development environment using ASP.NET MVC and Ruby on Rails. We
In c# you can return an integer to the underlying caller using Environment.Exit(n) (which
Using Linux environment with java,I'm having the config file which should be configured before
I have created a text file in Unix environment using Java code. For writing
I am currently developing a large and complex thermo-hydraulic systems in Modelica/Dymola environment using
I am writting an application that has multiple threads in Linux environment using C
Today I have started with setting up of Android Development Environment using Eclipse for
I have setup a Continuous Integration environment using CruiseControl.Net. I want to know how
Enviroment: Visual Studio 2012, MVC4, Razor, Internet Application. I have a code with search

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.