gpt4 book ai didi

c# - 我无法从我的 ASP.NET MVC3 ViewModel 中获取月、日、年下拉列表

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:33 25 4
gpt4 key购买 nike

我正在从 Webforms 转换到 MVC 3,但自学时遇到了一些麻烦。我创建了一个 ViewModel,它应该在我的 _Header 中用月、日、年值填充 3 个单独的选择列表。我想我有一个结构问题。最终结果是我在我的机器上的 www 服务中导致了一个未处理的异常,并且 Visual Studio 弹出并希望我调试它。任何帮助将不胜感激!

这是我的代码。

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:

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

_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:

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

最佳答案

我觉得您的模型太复杂了……我觉得有一个蛾日和年的 View 模型太过分了。我只是将它们实现为属性。以下是我将如何实现这一点:

模型:

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

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);
}

和 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" />
}

关于c# - 我无法从我的 ASP.NET MVC3 ViewModel 中获取月、日、年下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com