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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:10:16+00:00 2026-06-13T04:10:16+00:00

I am developping an MVC 4 application and I have some troubles. Here is

  • 0

I am developping an MVC 4 application and I have some troubles.
Here is an extract of my model:

public class RegistryModel
{
    [DisplayName("Registry id")]
    public  int registryId { get; set; }

    [Required]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
    [DisplayName("Reception date")]
    public  DateTime? receivedDate { get; set; }

    [Required]
    [DisplayName("Source")]
    public  Source source { get; set; }
}

Source object:

public class Source
{
    public virtual string sourceCode { get; set; }
    public virtual string fullName { get; set; }
    public virtual string shortName { get; set; }
    public virtual string type { get; set; }
    public virtual IList<Registry> registryList { get; set; }
}

Controller:

public ActionResult Create()
{
    var sourceRepo = new Repository<DTOS.Source>(MvcApplication.UnitOfWork.Session);
    IEnumerable<SelectListItem> sourcesEnum = sourceRepo.FilterBy(x=>x.type.Equals("C")).Select(c => new SelectListItem { Value = c.sourceCode, Text = c.fullName });
    ViewBag.Sources = sourcesEnum;
    return View();
}

And finally the view

@model Registry.Models.RegistryModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

<fieldset>
    <legend>New Registry</legend>      

    <div class="editor-label">
        @Html.LabelFor(model => model.source)
    </div>
    <div class="editor-field">
         @Html.DropDownListFor(Model => Model.source.sourceCode, (IEnumerable<SelectListItem>)ViewBag.Sources, String.Empty)
         @Html.ValidationMessageFor(model => model.source)
    </div>

If I select a source from the dropdownlist, it works fine. However, if I submit it without selecting any, I don’t have the error message, even if it is annotated as [Required] in the model.

After debugging the HttpPost Create at controller level, I see that the source member of the returned RegistryModel is instanciated, but all its member are null (sourceCode, fullName, etc).

Why is MVC instanciating the source member of the model if I did not select any source in the dropdownlist before submitting the form ?

I tried to modify the vue by this:

@Html.DropDownListFor(Model => **Model.source**, (IEnumerable<SelectListItem>)ViewBag.Sources, String.Empty)

This time I have the error message if I submit without selecting any source, but if I select one, I have another error message after submitting, saying ‘The value user code selected before submitting is invalid’

Any help would be most appreciated !
B.

  • 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-13T04:10:17+00:00Added an answer on June 13, 2026 at 4:10 am

    I believe the model binder will create a Source object and then try to fill in the values it has (whether there or not) when submitting the form. The RequiredAttribute is not acting as you may thin. See here:

    http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html

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

Sidebar

Related Questions

I've been developing an application using asp.net MVC, and I have some configurations that
I'm trying to write some tests for an MVC application we're developing. We have
Im currently developing a MVC application Framework and I have come for some advise
Friends, I have a question here. I'm developing an VS2010 - MVC 3 Application
I'm developing an MVC application and I have a routine that gets the currently
I have been developing an asp.net mvc application where i need to make large
I am developing an ASP.Net MVC 3 Web application using Razor Views. I have
I am developing an application in asp.net mvc where i need to have Chat
I am developping a web application by using the ASP .NET MVC 3 framework.
I'm developing Asp.MVc 2 application. In solution i've about 10 projects and i have

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.