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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:56:55+00:00 2026-06-15T14:56:55+00:00

I’m newbie developing with asp.net mvc 4, when I edit one of my models

  • 0

I’m newbie developing with asp.net mvc 4, when I edit one of my models I get that ModelState.IsValid is always returning false. My model is the next:

public class ShowTime
{
    public int ID { get; set; }

    [Display(Name = "Date")]
    [Required(ErrorMessage = "Date is required")]
    public DateTime Date { get; set; }

    [Display(Name = "Time")]
    [Required(ErrorMessage = "Time is required")]
    public DateTime DateTime { get; set; }

    public virtual Place Place { get; set; }
}

public class Place
{
    public int ID { get; set; }

    [Display(Name = "Place name")]
    [Required(ErrorMessage = "Place name is required")]
    public string Name { get; set; }

    public virtual Address Address { get; set; }
}

Then I have the next form to edit ShowTimes:

<fieldset class="formulari">
    <p>
        @Html.LabelFor(model => model.Date)
        @Html.EditorFor(model => model.Date)
        @Html.ValidationMessageFor(model => model.Date)
    </p>

    <p>
        @Html.LabelFor(model => model.DateTime)
        @Html.EditorFor(model => model.DateTime)
        @Html.ValidationMessageFor(model => model.DateTime)
    </p>            
    <p>
        <label>Escenari</label>
        @Html.DropDownListFor(model => model.Place.ID, new SelectList(new PlaceBLL().GetAll(), "ID", "Name"))
    </p>
    <p>
        @Html.LabelFor(model => model.Ticket.Price)
        @Html.EditorFor(model => model.Ticket.Price, "Ticket", new ViewDataDictionary(Html.ViewDataContainer.ViewData) { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Ticket" } })
        @Html.ValidationMessageFor(model => model.Ticket.Price)
    </p>
    <p>
        @Html.LabelFor(model => model.Ticket.BuyingTicketURL)
        @Html.EditorFor(model => model.Ticket.BuyingTicketURL)
        @Html.ValidationMessageFor(model => model.Ticket.BuyingTicketURL)
    </p>            

    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.Ticket.ID)

    <br />
    <input type="submit" value="Save" />
</fieldset>

The problem here is with Place object. User choose the Place of ShowTime from a Drop drown list and when user click on save button the model is returned and ModelState.IsValid return false, because that Place object from ShowTime in Model is getting its value from a DropDownList and only ID Property is filled (remember Name property of place is Required), validating the ModelState it fails because property Name of Place is empty.

How can I make the model valid if I’m getting the place object from a drop down list and Place object is not filled with all its properties? I’m believe that in that case I’m doing wrong mapping my database model exactly on a view, perphaps a better solution it would be to create a ViewModel like this, and transform the model to ShowTime on server code

public class ShowTimeViewModel
{
    public int ID { get; set; }

    [Display(Name = "Date")]
    [Required(ErrorMessage = "Date is required")]
    public DateTime Date { get; set; }

    [Display(Name = "Time")]
    [Required(ErrorMessage = "Time is required")]
    public DateTime DateTime { get; set; }

    public int PlaceID { get; set; }

    public Ticket Ticket { get; set; }
}

Other alternatives ?

  • 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-15T14:56:56+00:00Added an answer on June 15, 2026 at 2:56 pm

    The Name is never sent to the server. That’s how the <select> element works. It only sends the selected value to the server (which is the ID). So use a view model and get rid of this Name property. Make the ID property a nullable integer and decorate with the Required attribute forcing the user to select a value inside the DropDown. Once you get the ID back in your POST action you could query your database in order to retrieve the corresponding Name if needed.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

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.