简体   繁体   English

我无法从我的ASP.NET MVC3 ViewModel获取我的月,日,年下拉列表

[英]I can't get my Month, Day, Year dropdown lists to work from my ASP.NET MVC3 ViewModel

I'm converting over to MVC 3 from Webforms and have been having some trouble teaching myself. 我正在从Webforms转换到MVC 3,并且在教自己时遇到了一些麻烦。 I created a ViewModel which should populate 3 separate select lists with Month, Day, Year values within my _Header. 我创建了一个ViewModel,它应该在我的_Header中填充3个单独的选择列表,其中包含Month,Day,Year值。 I think I have a structure issue. 我想我有结构问题。 The end result is that I cause an unhandled exception in the www service on my machine and Visual Studio pops up and wants me to debug it. 最终结果是我在我的机器上的www服务中导致了未处理的异常,并且Visual Studio弹出并希望我调试它。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Here's my code. 这是我的代码。

DateSearchViewModel.cs DateSearchViewModel.cs

namespace DH.ViewModels
{
    public class DateSearchViewModel
    {
        //MONTH DROPDOWN LIST
        public class MonthViewModel
        {
           public int ID { set; get; }
           public string Month { set; get; }
        }       

        private List<MonthViewModel> _months;
        public List<MonthViewModel> Months
        {
            get
            {
                if (_months == null)
                {
                    _months = new List<MonthViewModel>();
                    _months.Add(new MonthViewModel { ID = 1, Month = "Jan" });
                    _months.Add(new MonthViewModel { ID = 2, Month = "Feb" });
                    _months.Add(new MonthViewModel { ID = 3, Month = "Mar" });
                    _months.Add(new MonthViewModel { ID = 4, Month = "Apr" });
                    _months.Add(new MonthViewModel { ID = 5, Month = "May" });
                    _months.Add(new MonthViewModel { ID = 6, Month = "Jun" });
                    _months.Add(new MonthViewModel { ID = 7, Month = "Jul" });
                    _months.Add(new MonthViewModel { ID = 8, Month = "Aug" });
                    _months.Add(new MonthViewModel { ID = 9, Month = "Sep" });
                    _months.Add(new MonthViewModel { ID = 10, Month = "Oct" });
                    _months.Add(new MonthViewModel { ID = 11, Month = "Nov" });
                    _months.Add(new MonthViewModel { ID = 12, Month = "Dec" });
                }
                return _months;
            }
        }
        public int SelectedMonthID { set; get; }


        //DAY DROPDOWN LIST
        public class DayViewModel
        {
            public int ID { set; get; }
            public string Day { set; get; }
        }
        private List<DayViewModel> _days;
        public List<DayViewModel> Days
        {
            get
            {
                if (_days == null)
                {
                    _days = new List<DayViewModel>();
                    _days.Add(new DayViewModel { ID = 1, Day = "1" });
                    _days.Add(new DayViewModel { ID = 2, Day = "2" });
                    _days.Add(new DayViewModel { ID = 3, Day = "3" });
                    _days.Add(new DayViewModel { ID = 4, Day = "4" });
                    _days.Add(new DayViewModel { ID = 5, Day = "5" });
                    _days.Add(new DayViewModel { ID = 6, Day = "6" });
                    _days.Add(new DayViewModel { ID = 7, Day = "7" });
                    _days.Add(new DayViewModel { ID = 8, Day = "8" });
                    _days.Add(new DayViewModel { ID = 9, Day = "9" });
                    _days.Add(new DayViewModel { ID = 10, Day = "10" });
                    _days.Add(new DayViewModel { ID = 11, Day = "11" });
                    _days.Add(new DayViewModel { ID = 12, Day = "12" });
                    _days.Add(new DayViewModel { ID = 1, Day = "13" });
                    _days.Add(new DayViewModel { ID = 2, Day = "14" });
                    _days.Add(new DayViewModel { ID = 3, Day = "15" });
                    _days.Add(new DayViewModel { ID = 4, Day = "16" });
                    _days.Add(new DayViewModel { ID = 5, Day = "17" });
                    _days.Add(new DayViewModel { ID = 6, Day = "18" });
                    _days.Add(new DayViewModel { ID = 7, Day = "19" });
                    _days.Add(new DayViewModel { ID = 8, Day = "20" });
                    _days.Add(new DayViewModel { ID = 8, Day = "21" });
                    _days.Add(new DayViewModel { ID = 8, Day = "22" });
                    _days.Add(new DayViewModel { ID = 8, Day = "23" });
                    _days.Add(new DayViewModel { ID = 8, Day = "24" });
                    _days.Add(new DayViewModel { ID = 8, Day = "25" });
                    _days.Add(new DayViewModel { ID = 8, Day = "26" });
                    _days.Add(new DayViewModel { ID = 8, Day = "27" });
                    _days.Add(new DayViewModel { ID = 8, Day = "28" });
                    _days.Add(new DayViewModel { ID = 8, Day = "29" });
                    _days.Add(new DayViewModel { ID = 8, Day = "30" });
                    _days.Add(new DayViewModel { ID = 8, Day = "31" });
                }
                return _days;
            }
        }
        public int SelectedDayID { set; get; }


        //YEAR DROPDOWN LIST
        public class YearViewModel
        {
            public int ID { set; get; }
            public string Year { set; get; }
        }
        private List<YearViewModel> _years;
        public List<YearViewModel> Years
        {
            get
            {
                if (_years == null)
                {
                    _years = new List<YearViewModel>();
                    _years.Add(new YearViewModel { ID = 1, Year = "2009" });
                    _years.Add(new YearViewModel { ID = 2, Year = "2010" });
                    _years.Add(new YearViewModel { ID = 3, Year = "2011" });
                    _years.Add(new YearViewModel { ID = 4, Year = "2012" });
                }
                return _years;
            }
        }    
        public int SelectedYearID { set; get; }
    }
}

CommonController.cs: CommonController.cs:

[ChildActionOnly]
public ActionResult _Header()
{
    DateSearchViewModel DateVM = new DateSearchViewModel();
    return View(DateVM);
}

_Header.cshtml: _Header.cshtml:

@model DH.ViewModels.DateSearchViewModel

@Html.DropDownListFor(x => x.SelectedMonthID, new SelectList(Model.Months, "ID", "Month"), "--Select--")
@Html.DropDownListFor(x => x.SelectedDayID, new SelectList(Model.Days, "ID", "Day"), "--Select--")
@Html.DropDownListFor(x => x.SelectedYearID, new SelectList(Model.Years, "ID", "Year"), "--Select--")

_Layout.cshtml: _Layout.cshtml:

@{Html.RenderAction("_Header", "Common");}

I feel like your model is overcomplicated...having a view model for moth day and year strikes me as overkill. 我觉得你的模型过于复杂......有一个蛾日和年的视图模型让我觉得有点过分。 I would just implement those as properties. 我只是将它们作为属性来实现。 Here's how I would accomplish this: 以下是我将如何实现这一目标:

The model: 该模型:

public class DateSearchViewModel {
  public int Day { get; set; }
  public int Month { get; set; }
  public int Year { get; set; }
}

The controller: 控制器:

public ActionResult _Header() {
  DateSearchViewModel DateVM = new DateSearchViewModel();
  return View(DateVM);
}
public ActionResult _Header( DateSearchViewModel vm ) {
  if( ModelState.IsValid ) {
    // save vm to database, etc.
  }
  return View(vm);
}

And the view: 并且观点:

@{
  var days = new List<SelectListItem>();
  for( var i = 1; i <= 31; i++ ) {
    days.Add( new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = Model.Day == i } );
  }
  var months = new List<SelectListItem> {
    new SelectListItem {Text = "Jan", Value = "1", Selected = Model.Month == 1},
    new SelectListItem {Text = "Feb", Value = "2", Selected = Model.Month == 2},
    new SelectListItem {Text = "Mar", Value = "3", Selected = Model.Month == 3},
    new SelectListItem {Text = "Apr", Value = "4", Selected = Model.Month == 4},
    new SelectListItem {Text = "May", Value = "5", Selected = Model.Month == 5},
    new SelectListItem {Text = "Jun", Value = "6", Selected = Model.Month == 6},
    new SelectListItem {Text = "Jul", Value = "7", Selected = Model.Month == 7},
    new SelectListItem {Text = "Aug", Value = "8", Selected = Model.Month == 8},
    new SelectListItem {Text = "Sep", Value = "9", Selected = Model.Month == 9},
    new SelectListItem {Text = "Oct", Value = "10", Selected = Model.Month == 10},
    new SelectListItem {Text = "Nov", Value = "11", Selected = Model.Month == 11},
    new SelectListItem {Text = "Dec", Value = "12", Selected = Model.Month == 12},
  };
  var years = new List<SelectListItem>();
  for( var i = 2009; i <= 2012; i++ ) {
    years.Add( new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = Model.Year == i } );
  }
}
@using( Html.BeginForm() ) {
  @Html.DropDownListFor( x => x.Month, months )
  @Html.DropDownListFor( x => x.Day, days )
  @Html.DropDownListFor( x => x.Year, years )
  <input type="submit" />
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使我的ViewModel在MVC ASP.NET中工作 - Cannot get my ViewModel to work in MVC ASP.NET ASP.NET MVC3无法将十进制字段从TextBoxFor绑定到我的模型 - ASP.NET MVC3 can't bind a decimal field from a TextBoxFor to my Model 如何将帐户信息从默认的ASPNETDB.mdf移到MyBase.sdf? ASP.Net MVC3? - How can I move account information from default ASPNETDB.mdf to my MyBase.sdf? ASP.Net MVC3? 无法让Jquery datepicker在我的asp.net mvc项目中工作 - Can't get Jquery datepicker to work in my asp.net mvc project 如何在ASP.NET MVC3中的URL中替换_? - How can I replace _ with - in my URL in ASP.NET MVC3? 如何在ASP.NET MVC3网站上对我的Json结果进行单元测试? - How can I unit test my Json result in an ASP.NET MVC3 web site? 我可以在ASP.NET MVC3应用程序中使用我的大部分代码吗? - Can I use most of my code in an ASP.NET MVC3 app? 在 ASP.NET MVC 中存储来自 3 DropDownListFor(日、月、年)的 DateTime - Store DateTime from 3 DropDownListFor (day, month, year) in ASP.NET MVC ASP.Net MVC3 Model vs ViewModel - 我不想写入数据库的属性 - ASP.Net MVC3 Model vs ViewModel - property that I don't want written to the database ASP.NET MVC5。 我不知道为什么我的表单返回空的viewmodel对象 - ASP.NET MVC5. I don't know why my form returns null viewmodel object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM