ASP MVC 2:POST 上的下拉列表出错
好吧,我是 ASP MVC2 的新手,我遇到了一些问题 htmlhelper 调用 Html.dropdownlistfor();
我想向用户展示一周中的几天列表。我希望将所选项目绑定到我的模型。
我创建了这个小类来生成一个天数列表+一个简短的符号,我将用它来将其存储在数据库中。
public static class days
{
public static List<Day> getDayList()
{
List<Day> daylist = new List<Day>();
daylist.Add(new Day("Monday", "MO"));
daylist.Add(new Day("Tuesday", "TU"));
// I left the other days out
return daylist;
}
public class Dag{
public string DayName{ get; set; }
public string DayShortName { get; set; }
public Dag(string name, string shortname)
{
this.DayName= name;
this.DayShortName = shortname;
}
}
}
我现在真的知道这是否是正确的方法
然后我将其放入我的控制器中:
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View("");
我的模型中有这一行
public string ChosenDay { get; set; }
在我的视图中显示列表:
<div class="editor-field">
<%: Html.DropDownListFor(model => model.ChosenDay, ViewData["days"] as SelectList, "--choose Day--")%>
</div>
现在这一切都很完美。第一次访问时,但是当我做 [HttpPost] 时 如下所示:
[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
// I removed some unrelated code here
// The code below executes when modelstate.isvalid == false
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View(model);
}
然后我将抛出以下异常:
The ViewData item that has the key 'ChosenDay' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
在我的视图中显示下拉列表的行处抛出此错误。
我真的不知道如何解决这个问题,并尝试了我在网上找到的几种解决方案。但它们都没有真正起作用。
提前!
Okay i'm new to asp mvc2 and i'm experiencing some problems with the
htmlhelper called Html.dropdownlistfor();
I want to present the user a list of days in the week. And i want the selected item to be bound to my model.
I have created this little class to generate a list of days + a short notation which i will use to store it in the database.
public static class days
{
public static List<Day> getDayList()
{
List<Day> daylist = new List<Day>();
daylist.Add(new Day("Monday", "MO"));
daylist.Add(new Day("Tuesday", "TU"));
// I left the other days out
return daylist;
}
public class Dag{
public string DayName{ get; set; }
public string DayShortName { get; set; }
public Dag(string name, string shortname)
{
this.DayName= name;
this.DayShortName = shortname;
}
}
}
I really have now idea if this is the correct way to do it
Then i putted this in my controller:
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View("");
I have this line in my model
public string ChosenDay { get; set; }
And this in my view to display the list:
<div class="editor-field">
<%: Html.DropDownListFor(model => model.ChosenDay, ViewData["days"] as SelectList, "--choose Day--")%>
</div>
Now this all works perfect. On the first visit, But then when i'm doing a [HttpPost]
Which looks like the following:
[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
// I removed some unrelated code here
// The code below executes when modelstate.isvalid == false
SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
ViewData["days"] = _list;
return View(model);
}
Then i will have the following exception thrown:
The ViewData item that has the key 'ChosenDay' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
This errors gets thrown at the line in my view where i display the dropdown list.
I really have no idea how to solve this and tried several solutions i found online. but none of them really worked.
Ty in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我见过这样的错误。
这是因为渲染视图时
ViewData["days"] as SelectList
为 null。这可能是因为 ViewData["days"] 为 null 或与 SelectList 的类型不同。问题必须在这里找到:
确保此代码
运行并且 ViewData["days"] 在返回 View 之前不为 null 且 IEnumerable。一定是因为 Model.IsValid 所以 ViewData["days"] 没有绑定。
I have seen such an error.
This is becouse
ViewData["days"] as SelectList
is null when the view is rendered. This can be becouse of ViewData["days"] is null or in different type then SelectList.The problem must be found here:
Make shure, that this code
runs and that ViewData["days"] not null and IEnumerable before you return View. It must be becouse of Model.IsValid so ViewData["days"] not binded.
当 HttpPost 控制器操作看到“EventRegistreerViewModel model”时,它将调用模型构造函数。
因此,如果您将代码添加到 EventRegistreerViewModel 模型中,如下所示:
然后在您的视图中使用此代码来显示列表和所选项目:
这样每次构建视图模型时,它将包含选择列表
The HttpPost controller action will call the model constructor when it sees "EventRegistreerViewModel model".
So if you add code to your EventRegistreerViewModel model like this:
Then in your view use this code to display the list and the selected item:
This way every time the view model is constructed it will include the select list