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

  • Home
  • SEARCH
  • 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 7611721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:43:58+00:00 2026-05-31T01:43:58+00:00

In a view I am adding an object of type person (for simplicity). However,

  • 0

In a view I am adding an object of type person (for simplicity). However, the validation for a dropdown of which car of all cars to choose (for example) will not show up before a post. Let me elaborate.

In my controller I setup a viewmodel to pass in. The populating is not a problem. Here are the simple versions of the models.

public class PersonViewModel
{
 public Person Person { get; set;}

 public List<Car> Cars { get; set;}
}

public class Car
{
 [Key]
 public int CarId { get; set;}
 [Required]
 public string Name { get; set;}
}

public class Person
{
 [Key]
 public int PersonId { get; set;}
 [Required]
 public int CarId { get; set;}
 [Required]
 public string FullName { get; set;}
}

Okay, pretty basic so far. In the view is the problem.

In the view:

@model PersonViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" 
type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" 
type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Add Person</legend>

    <div class="editor-label">Car</div>
    <div class="editor-field">
        @Html.DropDownListFor(
            m => m.Person.CarId,
            new SelectList(
                Model.Cars,
                "CarId",
                "Name",
                0
            ),
            "-- Select A Car --"
        )
        @Html.ValidationMessageFor(m=> m.Person.CarId)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m=> m.Person.FullName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Person.FullName)
        @Html.ValidationMessageFor(model => model.Person.FullName)
    </div>

    <p>
        <input type="submit" value="Create Person" />
    </p>
</fieldset>
}

When this view is rendered, a form to create a person is created. In the form, a dropdownfor list is generated with car names, and a person’s name field is available. If a car is selected and a person’s name is filled in, then create is clicked, the post goes through with no problems.

However, here is the discrepancy. If a person’s name is not filled in and create is clicked then the input box is highlighted red with the message saying name is required. If a car is not chosen, then the form will submit. In the controller, this is easy to catch under if(ModelState.isValid), but I would like to point out to the user that their person has no car. Adding the field [Required] public int CarId to the viewmodel and then referencing that through the binding will stop the form from being submitted if no car is chosen but seems to be a hacky solution.

The reason for the validation not being checked is that for some reason the generated <select> html object is lacking the added decoration class="input-validation-error" data-val-required="The field is required." data-val-number="The field must be a number." data-val="true".

How can I make sure this decoration is included?

  • 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-31T01:43:59+00:00Added an answer on May 31, 2026 at 1:43 am

    I guess that DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes is somehow set to false. This means that the Required attribute won’t be automatically added for value types such as integers. If you set it to true, the required attribute will be automatically added to the metadata of all value types and the HTML5 data-* attributes will be emitted. Its default value is true though.

    To avoid this kind of problems you could use a real view model:

    public class CreatePersonViewModel
    {
        public PersonViewModel Person { get; set;}
        public List<CarViewModel> Cars { get; set;}
    }
    
    public class CarViewModel
    {
        public int CarId { get; set;}
    
        public string Name { get; set;}
    }
    
    public class PersonViewModel
    {
        [Required]
        public int? CarId { get; set;}
    
        [Required]
        public string FullName { get; set;}
    }
    

    Notice that we no longer need a PersonId property for this view model since we are creating a new person which doesn’t have an id assigned yet. Also notice that the CarId is declared as nullable integer and decorated with the Required attribute. Since in our dropdownlist we allow for a default value (-- Select A Car --) we must properly reflect that on the view model by using a nullable type.

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

Sidebar

Related Questions

I am in need of adding an item of type IVehicle which is injected
I am adding object exercise to object session (as a relationship). In a view,
Does C# (WinForms) support adding a view or control to another control? Could anyone
I'm having a memory problem related to UIImageView. After adding this view to my
I'm adding an Image View in Interface Builder with a transparent PNG (A logo
I'm adding a new field to a list and view. To add the field
I can't view header in JTable while adding it into a JFrame.. String[] col={Name,ID,Marks};
I am working on adding a custom accessory view, (a button) to a UITableViewCell,
I have a table view that I am customizing and I am adding a
I have a root view controller with no nib file, I tried adding 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.