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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:27:51+00:00 2026-05-26T19:27:51+00:00

Whenever there is some custom validation to do or any fiddling with ModelState I

  • 0

Whenever there is some custom validation to do or any fiddling with ModelState I had to rely on magic strings (that reflect property names) so far. Surely there must be a better way.

say you have a form with 2 submit buttons. You’ve already set validation rules by adding Required attributes:

public class MyModel
{
    [Required]
    public string ValueOne { get; set; }
    [Required]
    public string ValueTwo { get; set; }
}

This will always validate both fields. But say I’ve added two buttons to the form showing and editor for above model. Button one only requires ValueOne and button two only required ValueTwo.

Typically I would have custom validation code that checks which button was clicked and do something along the lines:

private void ValidateViewModel(MyModel viewModel)
{
    foreach (var key in ModelState.Keys)
        ModelState[key].Errors.Clear();
    if (Request[{ButtonOneName}] != null && string.IsNullOrEmpty(viewModel.ValueOne))
        ModelState.AddModelError("ValueOne", "Required");
    else if (Request[{ButtonTwoName}] != null && string.IsNullOrEmpty(viewModel.ValueTwo))
        ModelState.AddModelError("ValueTwo", "Required");
}

Not very pretty, I know but … My beef is with magic string “ValueOne” and “ValueTwo”. Also with the way errors are cleared out. Is there a way to generate those keys? I’m looking for something like:

ModelState.KeyFor<MyModel>(m => m.ValueOne)

And then a logical extension:

ModelState.Get<MyModel>(m => m.ValueOne)

Before I start reinventing the wheel – is there something like this hidden somewhere already?

Before you ask, I normally define static class SubmitActions, containing constant strings that represent submit button names. And no, I can’t split it up into multiple forms because of the was the view is rendered.

Thanks for any suggestions.

  • 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-26T19:27:52+00:00Added an answer on May 26, 2026 at 7:27 pm

    There is a Model Validation Improvements in MVC 3, which will make you be able to check validate based on property related to each other

    So by using the IValidatableObject interface built-into .NET 4 to implement a custom validation method on a class. This method can apply validation rules across multiple properties and yield back multiple validation errors,

    Have you look at it?

    Model Validation Improvements part

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

Sidebar

Related Questions

Is there a way to run some custom Javascript whenever a client-side ASP.NET validator
Is there any way to enable horizontal scroll-bar whenever necessary? The situation was as
Is there a way to have a file that is modified / touched whenever
The C# compiler requires that whenever a custom type defines operator == , it
We have implemented some custom tooltip-drawing code that fires on Tick events of a
I have configured a SMTP appender to send an email whenever there's an error.
I don't want to change the java code whenever there is addition of tag
Is it possible to make sure my thread reacts on whenever there is network
I have a Flash app with a PHP backend. Whenever there is a PHP
Whenever I design a database, I always wonder if there is a best way

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.