Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Razor DropDownList中的参数无效_C#_Asp.net Mvc_Asp.net Mvc 4_Razor - Fatal编程技术网

C# Razor DropDownList中的参数无效

C# Razor DropDownList中的参数无效,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我收到一个错误,dropdownlist的参数无效。奇怪的是,我有一个结构类似的dropdownlist,运行良好,没有错误。带有错误的下拉列表是显示年份的第二个下拉列表: 视图: 控制器: [HttpPost] public ActionResult AddEmploymentHistory(int ApplicantID, int RecordNum) { this.ViewBag.RecordNum = RecordNum;

我收到一个错误,dropdownlist的参数无效。奇怪的是,我有一个结构类似的dropdownlist,运行良好,没有错误。带有错误的下拉列表是显示年份的第二个下拉列表:

视图:

控制器:

        [HttpPost]
    public ActionResult AddEmploymentHistory(int ApplicantID, int RecordNum)
    {
        this.ViewBag.RecordNum = RecordNum;
        this.ViewBag.StateList = this.GetStateList();
        this.ViewBag.MonthList = this.GetMonthList();
        this.ViewBag.YearList = this.GetYearList();
        return PartialView("_EmploymentHistoryPartial");
    }

        private IEnumerable<SelectListItem> GetYearList()
    {
        List<SelectListItem> yearList = new List<SelectListItem>();
        int current_yr = Convert.ToInt32(DateTime.Now.Year.ToString());
        int select_yr = 0;
        for(int i = (current_yr-3); i <= current_yr; i++)
        {
            select_yr = current_yr - (i - (current_yr-3));
            yearList.Add(new SelectListItem() { Value = select_yr.ToString(), Text = select_yr.ToString() });
        }
        return yearList;
    }

        private IEnumerable<SelectListItem> GetMonthList()
    {
        List<SelectListItem> monthList = new List<SelectListItem>();
        monthList.Add(new SelectListItem() { Value = "JAN", Text = "Jan" });
        monthList.Add(new SelectListItem() { Value = "FEB", Text = "Feb" });
        monthList.Add(new SelectListItem() { Value = "MAR", Text = "Mar" });
        monthList.Add(new SelectListItem() { Value = "APR", Text = "Apr" });
        monthList.Add(new SelectListItem() { Value = "MAY", Text = "May" });
        monthList.Add(new SelectListItem() { Value = "JUN", Text = "Jun" });
        monthList.Add(new SelectListItem() { Value = "JUL", Text = "Jul" });
        monthList.Add(new SelectListItem() { Value = "AUG", Text = "Aug" });
        monthList.Add(new SelectListItem() { Value = "SEP", Text = "Sep" });
        monthList.Add(new SelectListItem() { Value = "OCT", Text = "Oct" });
        monthList.Add(new SelectListItem() { Value = "NOV", Text = "Nov" });
        monthList.Add(new SelectListItem() { Value = "DEC", Text = "Dec" });
        return monthList;
    }

因此,我使用您提供的数据创建了一个示例项目,并得出以下结论:

控制器

我愿意

   public ActionResult Index()
        {
            var viewModel = new indexViewModel{
            RecordNum = 1,
            MonthList = _someService.GetMonthList(),
            YearList = _someService.GetYearList()
            }
            return PartialView(viewModel);
        }
在你看来,你不必担心魔术弦。你可以简单地做

@Model.MonthList

如果我不得不猜测,这与加载页面后将viewbag用于部分视图有关。Viewbag有其用途,但不建议用于下拉列表。我建议您为局部视图构建视图模型

public Class EmploymentHistory{
    public EmploymentHistory(){
        StateList = new List<SelectListItem>();
        MonthList = new List<SelectListItem>();
        YearList = new List<SelectListItem>();
    }
    public string RecordNum { get; set; }
    public List<SelectListItem> StateList { get; set; }
    public string SelectedState { get; set; }
    public List<SelectListItem> MonthList { get; set; }
    public string SelectedMonth { get; set; }
    public List<SelectListItem> YearList { get; set; }
    public string SelectedYear { get; set; }
}
@model EmploymentHistory
您可以为控制器上的下拉列表设置默认值

eh.SelectedState = //Default value;
return PartialView("_EmploymentHistoryPartial", eh);
在你的部分

public Class EmploymentHistory{
    public EmploymentHistory(){
        StateList = new List<SelectListItem>();
        MonthList = new List<SelectListItem>();
        YearList = new List<SelectListItem>();
    }
    public string RecordNum { get; set; }
    public List<SelectListItem> StateList { get; set; }
    public string SelectedState { get; set; }
    public List<SelectListItem> MonthList { get; set; }
    public string SelectedMonth { get; set; }
    public List<SelectListItem> YearList { get; set; }
    public string SelectedYear { get; set; }
}
@model EmploymentHistory
然后将下拉列表更改为

@Html.DropDownList(SelectedState, model.StateList, new { @class...


他声称第一个下拉菜单是有效的,所以我不认为viewbag和partial会有问题。虽然我同意在viewModel中使用它是最好的。但我能够使用您的解决方案找到问题。我已经将EmploymentStartYear定义为int,而EmploymentStartMonth是字符串。将int改为string解决了这个问题。在没有解释的情况下对问题进行向下投票是没有意义的。我不知道我的问题有什么不正确或不好的地方。我想投票被否决是因为缺乏信息。在我读到标题之前,我以为你没有包括错误。关于异常(包括内部异常)的更多信息对于这类问题非常有用。我同意错误会很好。我也同意不作解释就否决投票是没有意义的。最初的问题包括他的代码,这是一个很好的开始。
eh.SelectedState = //Default value;
return PartialView("_EmploymentHistoryPartial", eh);
@model EmploymentHistory
@Html.DropDownList(SelectedState, model.StateList, new { @class...
@Html.DropDownListFor(x => x.SelectedState, model.StateList, new { @class...