Razor DropDownList中的参数无效

时间:2014-06-24 19:26:34

标签: c# asp.net-mvc asp.net-mvc-4 razor

我收到一个错误,即下拉列表包含无效参数。奇怪的是,我有一个类似结构的下拉列表,工作正常,没有错误。带错误的下拉列表是第二个(显示年份):

查看:

@{
                        string month = "TempEmployments[" + idx + "].EmploymentStartMonth";
                        string year = "TempEmployments[" + idx + "].EmploymentStartYear";
                     }
                    @Html.DropDownList(month, (IEnumerable<SelectListItem>)this.ViewBag.MonthList,EmploymentStartMonth, new { @class = "field panel-field EmploymentDate", @id = month })
                    @Html.ValidationMessageFor(model => model.EmploymentStart)
                    @Html.DropDownList(year, (IEnumerable<SelectListItem>)this.ViewBag.YearList, EmploymentStartYear, new { @class = "field panel-field EmploymentDate", @id = year })

控制器:

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

2 个答案:

答案 0 :(得分:0)

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

<强>控制器

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            this.ViewBag.RecordNum = 1;
            this.ViewBag.MonthList = this.GetMonthList();
            this.ViewBag.YearList = this.GetYearList();
            return PartialView();
        }

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

    }
}

查看

@{
    //string month = "TempEmployments[" + idx + "].EmploymentStartMonth";
    //string year = "TempEmployments[" + idx + "].EmploymentStartYear";
}
@Html.DropDownList("some name", (IEnumerable<SelectListItem>)this.ViewBag.MonthList,"some label", new { @class = "field panel-field EmploymentDate", @id = "id" })
<!-- @@Html.ValidationMessageFor(model => model.EmploymentStart) No idea what your model is-->
@Html.DropDownList("some name2", (IEnumerable<SelectListItem>)this.ViewBag.YearList, "some label", new { @class = "field panel-field EmploymentDate", @id = "id2" })

我的测试中的两个下拉列表都100%正常。这让我相信没有正确填充id,name或label。您提供的代码未显示其值的创建方式

像我一样填充你的第二个下拉列表(带有虚拟ID,标签和名称,看看它是否呈现。然后通过消除过程将它们添加回来。

另外,只是一般性提示,我会将这些类从控制器中抽象出来并放入自己的类中。有助于保持清洁。以及传递视图模型以及所需的所有属性。这避免了“魔术字符串”,并允许代码更容易更新和调试。

作为示例而不是:

   public ActionResult Index()
        {
            this.ViewBag.RecordNum = 1;
            this.ViewBag.MonthList = this.GetMonthList();
            this.ViewBag.YearList = this.GetYearList();
            return PartialView();
        }

我愿意

   public ActionResult Index()
        {
            var viewModel = new indexViewModel{
            RecordNum = 1,
            MonthList = _someService.GetMonthList(),
            YearList = _someService.GetYearList()
            }
            return PartialView(viewModel);
        }

然后在您的视图中,您不必担心魔术字符串。你可以简单地做到

@Model.MonthList

答案 1 :(得分:0)

如果我不得不猜测它与在页面加载后用于局部视图的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; }
}

然后在您的控制器上而不是设置视图包设置模型

EmploymentHistory eh = new EmploymentHistory();
eh.StateList = GetStateList();
etc...

您可以设置控制器上下拉菜单的默认值

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...