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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:47:58+00:00 2026-05-22T20:47:58+00:00

So, I am trying to create a DropDownList that will edit the various member

  • 0

So, I am trying to create a DropDownList that will edit the various member variables the DateTime class has (which I have an instance of in my model), such as Day, Month, and Year. However, when an item is selected in the DropDownList and the Save input button is clicked, the data does not save. All other edited pieces of data will be changed and saved, but the DateTime field will just not update. I’d rather not make a new model just for my Dates, but it can be done. I can create the SelectList, I do so in an HTML Helper, shown below:

namespace ErrorReport.Helpers
{
public class DateList
{
    public static IEnumerable<SelectListItem> DayList
    {
        get
        {
            var days = new List<SelectListItem>();
            for (int i = 1; i < 32; i++)
            {
                days.Add(new SelectListItem
                {
                    Value = i.ToString(),
                    Text = i.ToString()
                });
            }
            return days;
        }
    }

There’s obviously two more Lists that get made, one for Year and one for Month (I’m not bothering with hours, minutes, or seconds), didn’t show them since the code is identical. In my View, my editor code looks like the below, and the variable I want to change is CmpD (of DateTime class):

<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>Edit Report: "@Model.Title"</legend>

    @Html.HiddenFor(model => model.ReportId)
    @Html.HiddenFor(model => model.SbmD)
    @Html.HiddenFor(model => model.UserName)
    @Html.HiddenFor(model => model.CmpD)

    ...irrelevant editor code...

    <div class="editor-label">
        @Html.LabelFor(model => model.CmpD, "Estimated Completion Date:")
    </div>

    <div class="editor-field">  

        @Html.DropDownListFor(model => model.CmpD.Month, DateList.MonthList)
        @Html.DropDownListFor(model => model.CmpD.Day, DateList.DayList)
        @Html.DropDownListFor(model => model.CmpD.Year, DateList.YearList)

        //@Html.ValidationMessageFor(model => model.CmpD.Month)
        //Above line was commented out
    </div>
    <p>
        <input type="submit" value="Save"/>
    </p>
</fieldset>
}

I did have validation in place for CmpD's member variables earlier (only the ones I wanted to change), commented them out because they kept throwing validation errors for every possible SelectList choice (marked in above code). I didn’t put arguments in the BeginForm function because they caused save problems with saving the other data. I also added the HiddenFor field for CmpD (which is my DateTime) to get everything to save properly, since without that line of code the return controller did not recognize the Model as valid and didn’t save it. I tried adding a HiddenFor field for every member variable in the DateTime class I am not using, and I still get Validation errors if I remove the HiddenFor(model => model.CmpD), even with other Hidden Fields present. I have also tried to make a list this way:

    public static IEnumerable<int> YearList
    {
        get
        {
            var years = new List<int>();
            for (int i = 0; i < 4; i++)
            {
                years.Add(i);
            }
            return years.AsEnumerable();
        }
    }

However, this prevents the Html.DropDownListFor function from working at all, it apparently has to use SelectList items. I have noticed that only strings can be SelectList items, and that the DateTime member variables are ints. Is that causing the problem? Because I don’t see where they are recasted to ints and cannot figure out how I would do that. Basically, how do I edit DateTime member variables in a DropDownList?

Also: Is this too much inline code? Thanks in advance!

  • 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-22T20:47:59+00:00Added an answer on May 22, 2026 at 8:47 pm

    The DateTime properties (Day, Month, etc.) are all read-only. Hence, it is not possible to set them on a DateTime instance.
    You would need settable int properties in your model to be able to update the values from the drop-downs. Then you can construct a DateTime from those values later. I would probably create a separate model class just for this purpose and implement the date editor as separate editor template or partial view that handles all the details (to keep your code clean).

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

Sidebar

Related Questions

I'm trying to create a web page that will display an appropriate user control
I currently have a class and I'm trying to create an easy GUI to
I am trying to create an autocomplete textbox using jquery which will be bound
I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I was trying to create a custom combobox that inherited from ComboBox, but I've
I'm trying to create some cascading drop downs. I have my States loading via

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.