ASP MVC 2:POST 上的下拉列表出错

发布于 2024-08-30 22:04:30 字数 1981 浏览 1 评论 0原文

好吧,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧竹 2024-09-06 22:04:30

我见过这样的错误。
这是因为渲染视图时 ViewData["days"] as SelectList 为 null。这可能是因为 ViewData["days"] 为 null 或与 SelectList 的类型不同。
问题必须在这里找到:

[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
}

确保此代码

SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
 ViewData["days"] = _list;

运行并且 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:

[HttpPost]
public ActionResult Registreer(EventRegistreerViewModel model)
{
}

Make shure, that this code

SelectList _list = new SelectList(Days.getDayList(), "DayShortName", "DayName");
 ViewData["days"] = _list;

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.

折戟 2024-09-06 22:04:30

当 HttpPost 控制器操作看到“EventRegistreerViewModel model”时,它将调用模型构造函数。

[HttpPost] 
public ActionResult Registreer(EventRegistreerViewModel model) 

因此,如果您将代码添加到 EventRegistreerViewModel 模型中,如下所示:

...
public IEnumerable<string> MySelectList { get; set; }
public EventRegistreerViewModel() {
    // build the select the list as selectList, then
    this.MySelectList = selectList;
}

然后在您的视图中使用此代码来显示列表和所选项目:

Html.DropDownListFor(model => model.ChosenDay, model.MySelectList, "--choose Day--")

这样每次构建视图模型时,它将包含选择列表

The HttpPost controller action will call the model constructor when it sees "EventRegistreerViewModel model".

[HttpPost] 
public ActionResult Registreer(EventRegistreerViewModel model) 

So if you add code to your EventRegistreerViewModel model like this:

...
public IEnumerable<string> MySelectList { get; set; }
public EventRegistreerViewModel() {
    // build the select the list as selectList, then
    this.MySelectList = selectList;
}

Then in your view use this code to display the list and the selected item:

Html.DropDownListFor(model => model.ChosenDay, model.MySelectList, "--choose Day--")

This way every time the view model is constructed it will include the select list

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文