MVC 4下拉框错误 [英] MVC 4 drop down box error

查看:72
本文介绍了MVC 4下拉框错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一天以上的时间出现此错误了,我似乎无法修复该错误.我知道网上有很多关于此主题的问题,这些问题我已经读了一遍又一遍,但仍未解决.我只是在学习MVC 4,所以我非常困惑.我收到错误消息:

I have had this error for over a day now and I really can't seem to fix it. I know there are a lot of questions on this topic online which I have read over and over and still haven't solved the issue. I'm just learning MVC 4 so I'm extremely confused. I get the error message:

具有键"cabinCrewId"的ViewData项的类型为"System.Int32",但必须类型为"IEnumerable".

The ViewData item that has the key 'cabinCrewId' is of type 'System.Int32' but must be of type 'IEnumerable'.

任何帮助或指导将不胜感激!

Any help or direction would be greatly appreciated!

我的控制器:

 public ActionResult AddCrew()
    {
        FlightCabinCrew fcc = new FlightCabinCrew();
        return View(fcc);
    }

发布动作:

[HttpPost]
    public ActionResult AddCrew(FlightCabinCrew fcc)
    {
        if (ModelState.IsValid)
        {
            using (A1Context db = new A1Context())
            {
                var data = from person in db.person
                           from flightcrew in db.flightcabincrew
                           from cabincrew in db.cabincrew
                           where flightcrew.cabinCrewId == cabincrew.person
                           where cabincrew.person == person.id
                           select person.name;

                ViewBag.list = new SelectList(data.ToList(), "id", "name");

                db.flightcabincrew.Add(fcc);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
        }
        else
        {
            using (A1Context db = new A1Context())
            {
                var data = from person in db.person
                           from flightcrew in db.flightcabincrew
                           from cabincrew in db.cabincrew
                           where flightcrew.cabinCrewId == cabincrew.person
                           where cabincrew.person == person.id
                           select person.name;

                ViewBag.list = new SelectList(data.ToList(), "name", "name");
                return View(fcc);
            }
        }
    }
}

我的观点:

<div class="editor-label">
        @Html.LabelFor(model => model.cabinCrewId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.cabinCrewId, (SelectList)ViewBag.list)
        @Html.ValidationMessageFor(model => model.cabinCrewId)
    </div>

谢谢

推荐答案

您需要在GET AddCrew 中将 SelectList 分配给 ViewBag 方法(就像您在POST方法中所做的一样).另请注意,如果 ModelState 是有效的,则无需在POST方法中分配 SelectList (您可以保存然后重定向,因此没有必要,因为您不返回视图)

You need to assign the SelectList to ViewBag in the GET AddCrew method (as you have done in the POST method). Note also you do not need to assign the SelectList in the POST method if ModelState is valid (your saving and then redirecting so its not necessary since your not returning the view)

这篇关于MVC 4下拉框错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆