C# 数据绑定不包含名称为 属性

C# Databinding does not contain property with name

我正在创建一个带有 MultiSelectList 的 ListBoxFor,但出现以下错误:DataBinding: *.Models.Facilities does not contain a property with the name FacilitiesList.

这是我的观点:

@model *.Models.Reservation

    <div class="form-group">
        @Html.LabelFor(model => model.FacilitiesList, "Facilities", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.ListBoxFor(model => model.FacilitiesList, ViewBag.FacilitiesList as MultiSelectList, htmlAttributes: new { @class = "form-control" })
        </div>
    </div>

viewbag FacilitiesList 填充如下:

public ActionResult Create()
    {
        ViewBag.FacilitiesList = new SelectList(_facilityrepository.GetAll(), "FacilitiesList", "FacilityType");
        return View();
    }

解决了。简单的解决方案:

我改变了这个:

ViewBag.FacilitiesList = new SelectList(_facilityrepository.GetAll(), "FacilitiesList", "FacilityType");

对此:

ViewBag.FacilitiesList = new SelectList(_facilityrepository.GetAll(), "FacilityId", "FacilityType");