【问题标题】:Getting Value cannot be null. Parameter name: items, MVC4 selectList error?获取值不能为空。参数名称:items,MVC4 selectList 错误?
【发布时间】:2014-04-08 03:46:09
【问题描述】:

我最近让下拉列表正常工作,一切都很好,但由于某种原因,它现在抛出 value cannot be null 异常。我已经交换了变量名,并试图解决错误但没有任何效果。您能否看一下我的代码并指出正确的方向。我是 MVC4 的初学者,现在已经研究了几个小时,所以任何帮助都将不胜感激。我只是提前发布了相关代码。

编辑:它在视图中@Html.DropDownListFor(....) 行的开头抛出错误

型号:

public partial class SiteBookingsTable
{        
    public string departureAirport { get; set; }
    public string arrivalAirport { get; set; }        

    [Required]
    [Display(Name = "Flying From")]
    public string chooseDepartureAirport { get; set; }

    [Required]
    [Display(Name = "Flying To")]
    public string chooseArrivalAirport { get; set; }
}

查看:

@model Project56.Models.SiteBookingsTable
@{
List<Project56.Models.SiteBookingsTable> selectDepFlight = ViewBag.depList;
}

<tr>
    <td>@Html.LabelFor(model => model.chooseDepartureAirport)<br /> 
       @Html.DropDownListFor(model => model.chooseDepartureAirport, new SelectList(selectDepFlight,"departureAirport","departureAirport"))</td>                    
</tr>

控制器:

    public ActionResult Create()
    {
        List<SiteBookingsTable> selectDepFlight = new List<SiteBookingsTable>();

        selectDepFlight.Add(new SiteBookingsTable() { listID = 0, departureAirport = "-Select-" });
        selectDepFlight.Add(new SiteBookingsTable() { listID = 1, departureAirport = "London (LTN)" });
        selectDepFlight.Add(new SiteBookingsTable() { listID = 2, departureAirport = "Manchester (MAN)" });

        ViewBag.depList = selectDepFlight;


        return View();
    }

    [HttpPost]
    public ActionResult Create(SiteBookingsTable aBooking)
    {
        if (ModelState.IsValid == true)
        {
            newBooking.SiteBookingsTables.Add(aBooking);
            newBooking.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(aBooking);
    }

【问题讨论】:

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


    【解决方案1】:

    在你的视图中你正在使用SelectList(selectDepFlight,

    但您发送的是 ViewBag 变量 ViewBag.depList = selectDepFlight;

    你想要SelectList(ViewBag.depList,

    虽然老实说它可能应该在模型中,而不是在 ViewBag 中

    【讨论】:

      【解决方案2】:

      如果我没记错的话,SelectList 类型必须是可枚举的。 --Source

      ** 编辑,最好从控制器端而不是在视图本身中将值作为可枚举返回。还要考虑使用 ViewModel 而不是模型本身。

      List<Project56.Models.SiteBookingsTable> selectDepFlight = ViewBag.depList;
      
      //Add the following line(s) to cast your list to an enumerable.
      
      IEnumerable<Project56.Models.SiteBookingsTable> enumSelectDepFlight = selectDepFlight.AsEnumerable<Project56.Models.SiteBookingsTable>
      
      //Alter the following code 
      @Html.DropDownListFor(model => model.chooseDepartureAirport, new SelectList(selectDepFlight,"departureAirport","departureAirport"))
      
      //to match the enumerable above
      
      @Html.DropDownListFor(model => model.chooseDepartureAirport, new SelectList(enumSelectDepFlight,"departureAirport","departureAirport"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-19
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 2013-09-13
        • 2013-03-23
        • 1970-01-01
        相关资源
        最近更新 更多