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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:52:49+00:00 2026-05-29T20:52:49+00:00

I have an MVC3 app, using EF, where one table winds up in a

  • 0

I have an MVC3 app, using EF, where one table winds up in a lot of view models. It has a field that is normally not required. But, there is one situation (and one view) where it is required. I want both client and server validation for this field’s required-ness, in the one place where it IS required.

Now, I do not want to put the Required data annotation on the field, because then it will always be required. This would be true even on views where the field isn’t displayed for edit, which would result in a validation message the user couldn’t make go away.

I also am not looking for a RequiredIf validation attribute, because that doesn’t fit the design. The field is only required under one circumstance, and I don’t want to have to stick in an extra field from a superfluous table in all my view models, just for the sake of the dependency.

All I want is to be able to turn on required field validation for one field in one view, in an ad-hoc, not-based-on-DataAnnotations manner.

The way that immediately suggested itself to me was to specify the necessary data-val-* attributes when creating the markup for the field (through the HtmlAttributes object at the end of the HtmlHelper.TextBoxFor signature). If I understand correctly, this should cue JS unobtrusive validation to treat the field as required.

But this is … a little too much of a hack, seems to me. Unobtrusive validation support would be intruding into the higher-level code. Is there a more elegant way to do this? A jQuery Validation function call I can use to make what I want to have happen, happen?

  • 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-29T20:52:50+00:00Added an answer on May 29, 2026 at 8:52 pm

    Not only does that data-val approach have a codes smell, it’s also unreliable because the end user may be using a client that doesn’t do unobtrusive validation. client-side validation is candy, that’s nice to have. You must always have server-side validation.

    This is the problem you have when you use your data model for your view model. The correct approach is to create seperate view models for each view. You can get by with sharing View Models and creating seperate view models for only those views that are different. In either case, if you have a unique view, it needs its own unique view model.

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

Sidebar

Related Questions

I have an ASP.NET MVC3 app that features a form with a nested-table input
I have an MVC3 C#.Net web app. In my HTML View, I am using
I have an MVC3 C#.Net web app. In my HTML View, I am using
I have a C#.Net MVC3 web app. I am using Drop Down lists all
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2
I have an asp.net mvc3 Razor app being developed in VS2010 using Git for
Have an ASP.NET MVC3 app with model validation using FluentValidation . User enters some
I have an ASP MVC3 application that has several HttpPost ActionResult methods. During a
I have a Spring MVC/3.0 app using tiles as it's view, this is working
I am using Ninject in MVC3 application. One of my resolvable dependencies makes use

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.