如何讓我的MultiSelectList綁定到我的數據模型?

asp.net-mvc asp.net-mvc-5 c# entity-framework entity-framework-6

我和汽車之間有許多關係。在我看來,我希望能夠為燈光等項目提供多選列表。我嘗試了很多不同的格式,我似乎無法獲得列表來設置所選項目。我喜歡的方法是@Html.ListBoxFor(model => model.enitity.relatedentity,Model.SomeSelectListItemList),但是我似乎無法獲取列表以在視圖中設置所選項目。

下面是我測試的代碼,我使用的是Asp.net MVC5,C#和實體框架6。

這是我的控制器動作

    // GET: Car/Edit/
    [Authorize(Roles = "Ecar")]
    public ActionResult Edit(int id)
    {


        CarsEditViewModel carEditViewModel = new CarsEditViewModel();

        carEditViewModel.Cars = unitOfWorkcar.CarRepository.FindIncluding(id);

        IList<Approval> approvalList = unitOfWorkcar.ApprovalRepository.All.ToList();
        IList<Connector> connectorList = unitOfWorkcar.ConnectorRepository.All.ToList();
        IList<InputVoltage> inputVoltagesList = unitOfWorkcar.InputVoltageRepository.All.ToList();



        carEditViewModel.ApprovalList = from c in approvalList select new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = true};

        carEditViewModel.Categories = new MultiSelectList(carEditViewModel.ApprovalList, "Value", "Text", "Selected");


       // ,carEditViewModel.ApprovalList.Select(c => c.Text),carEditViewModel.ApprovalList.Select(c => c.Selected)

        //carEditViewModel.ApprovalList = from c in approvalList select new MultiSelectList( //{ Selected = (carEditViewModel.Cars.Approvals.Any(app => app.Id == c.Id)) , Text = c.Name, Value = c.Id.ToString() };
       // carEditViewModel.ConnectorList = from c in connectorList select new SelectListItem { Selected = true,  Text = c.Name, Value = c.Id.ToString() };
        carEditViewModel.InputVoltageList = from c in inputVoltagesList select new SelectListItem { Text = c.Name, Value = c.Id.ToString() };



        return View(carEditViewModel);

    }

這是我的看法

    // GET: Car/Edit/
    [Authorize(Roles = "Ecar")]
    public ActionResult Edit(int id)
    {


        CarsEditViewModel carEditViewModel = new CarsEditViewModel();

        carEditViewModel.Cars = unitOfWorkcar.CarRepository.FindIncluding(id);

        IList<Approval> approvalList = unitOfWorkcar.ApprovalRepository.All.ToList();
        IList<Connector> connectorList = unitOfWorkcar.ConnectorRepository.All.ToList();
        IList<InputVoltage> inputVoltagesList = unitOfWorkcar.InputVoltageRepository.All.ToList();



        carEditViewModel.ApprovalList = from c in approvalList select new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = true};

        carEditViewModel.Categories = new MultiSelectList(carEditViewModel.ApprovalList, "Value", "Text", "Selected");


       // ,carEditViewModel.ApprovalList.Select(c => c.Text),carEditViewModel.ApprovalList.Select(c => c.Selected)

        //carEditViewModel.ApprovalList = from c in approvalList select new MultiSelectList( //{ Selected = (carEditViewModel.Cars.Approvals.Any(app => app.Id == c.Id)) , Text = c.Name, Value = c.Id.ToString() };
       // carEditViewModel.ConnectorList = from c in connectorList select new SelectListItem { Selected = true,  Text = c.Name, Value = c.Id.ToString() };
        carEditViewModel.InputVoltageList = from c in inputVoltagesList select new SelectListItem { Text = c.Name, Value = c.Id.ToString() };



        return View(carEditViewModel);

    }

}

這是我的viewmodel

    // GET: Car/Edit/
    [Authorize(Roles = "Ecar")]
    public ActionResult Edit(int id)
    {


        CarsEditViewModel carEditViewModel = new CarsEditViewModel();

        carEditViewModel.Cars = unitOfWorkcar.CarRepository.FindIncluding(id);

        IList<Approval> approvalList = unitOfWorkcar.ApprovalRepository.All.ToList();
        IList<Connector> connectorList = unitOfWorkcar.ConnectorRepository.All.ToList();
        IList<InputVoltage> inputVoltagesList = unitOfWorkcar.InputVoltageRepository.All.ToList();



        carEditViewModel.ApprovalList = from c in approvalList select new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = true};

        carEditViewModel.Categories = new MultiSelectList(carEditViewModel.ApprovalList, "Value", "Text", "Selected");


       // ,carEditViewModel.ApprovalList.Select(c => c.Text),carEditViewModel.ApprovalList.Select(c => c.Selected)

        //carEditViewModel.ApprovalList = from c in approvalList select new MultiSelectList( //{ Selected = (carEditViewModel.Cars.Approvals.Any(app => app.Id == c.Id)) , Text = c.Name, Value = c.Id.ToString() };
       // carEditViewModel.ConnectorList = from c in connectorList select new SelectListItem { Selected = true,  Text = c.Name, Value = c.Id.ToString() };
        carEditViewModel.InputVoltageList = from c in inputVoltagesList select new SelectListItem { Text = c.Name, Value = c.Id.ToString() };



        return View(carEditViewModel);

    }

}

熱門答案

發布多對多關係的值的最大問題是在模型上沒有要綁定的直接字段。這是視圖模型變得非常方便的地方,您已經在使用它,但不是以正確的方式。

首先你需要你的SelectList ,它實際上只是一個IEnumerable<SelectListItem> 。這將包含所有可用選項,這很容易。所以在你的視圖模型中:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }

在你的行動中:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }

請注意,我沒有設置Selected :我們將讓HtmlHelper處理它。我也沒有處理MultiSelectList

現在,您還需要回復一些內容,因為您的值將是id,我們將使用List<int> ,因此在您的視圖模型中:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }

這裡有一點點。屬性的set方法很簡單:當我們返回一個已發布的值時,只需將selectedCategories設置為該值即可。 get有點複雜:在這裡我們需要壓縮你的類別對象列表(這裡稱為Categories ,因為我不知道它實際來自哪裡)到這些類別的id的簡單列表中。

現在,在您看來:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }

這就是你所需要的一切。您正在使用ListBox控件,因此它已經是一個多選列表。並且,通過將其綁定到所有當前選定的ID的列表,它知道從Model.CategoryChoices獲取的SelectListItem列表中自動選擇哪些項目。

在您的帖子操作中,您需要將這些ID轉換為其關聯對象:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }

然後,您可以手動將模型的類別設置為此新列表:

public IEnumerable<SelectListItem> CategoryChoices { get; set; }



許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因