Bob*_*Bob 7 c# asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5
我和汽车之间有许多关系.在我看来,我希望能够为灯光等项目提供多选列表.我尝试了很多不同的格式,我似乎无法获得列表来设置所选项目.我喜欢的方法是@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);
}
Run Code Online (Sandbox Code Playgroud)
这是我的看法
@model NewBobPortal.ViewModels.CarsEditViewModel
@using (Html.BeginForm())
{
@* @Html.ListBoxFor("SelectedApprovals",model => model., new { @class = "multiselect" })*@
@*@Html.ListBoxFor(model => model.Cars.Approvals, Model.ApprovalList)
@Html.ListBoxFor(model => model.Cars.Connectors,Model.ConnectorList, new {Multiple = "multiple"})
@Html.ListBoxFor(model => model.ConnectorList, Model.ConnectorList)*@
@*@Html.ListBox("test",Model.Cars.InputVoltages, Model.InputVoltageList)*@
@Html.DropDownList("somethingelse", new MultiSelectList(Model.InputVoltageList, "Value", "Text", Model.InputVoltageList.Select(c => c.Value)), new { multiple = "multiple" })
@Html.DropDownListFor(model => model.Cars.InputVoltages , new MultiSelectList(Model.LensColorList, "Value", "Text", Model.LensColorList.Select(c => c.Value)), new { multiple = "multiple" })
@Html.ListBoxFor(m => m.Cars.Approvals, Model.Categories)
<p>
<input type="submit" value="Save" />
</p>
Run Code Online (Sandbox Code Playgroud)
}
这是我的viewmodel
using NewBobPortal.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace NewBobPortal.ViewModels
{
public class CarsEditViewModel
{
public Car Cars { get; set; }
public IEnumerable<SelectListItem> ApprovalList { get; set; }
public IEnumerable<MultiSelectList> ConnectorList { get; set; }
public IEnumerable<SelectListItem> InputVoltageList { get; set; }
public MultiSelectList Categories { get; set; }
public IEnumerable<Approval> SelectedCategories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
}
发布多对多关系的值的最大问题是在模型上没有要绑定的直接字段.这就是视图模型变得非常方便的地方,您已经在使用它,但不是以正确的方式.
首先,你需要你的SelectList,实际上只是一个IEnumerable<SelectListItem>.这将包含所有可用选项,这很容易.所以在你的视图模型中:
public IEnumerable<SelectListItem> CategoryChoices { get; set; }
Run Code Online (Sandbox Code Playgroud)
在你的行动中:
carEditViewModel.CategoryChoices = approvalList.Select(m => new SelectListItem {
Text = c.Name,
Value = c.Id.ToString()
});
Run Code Online (Sandbox Code Playgroud)
请注意,我没有设置Selected:我们将让HtmlHelper处理它.我也没有处理MultiSelectList过.
现在,您还需要回复一些内容,因为您的值将是ID,我们将使用a List<int>,因此在您的视图模型中:
private List<int> selectedCategories;
public List<int> SelectedCategories
{
get
{
if (selectCategories == null)
{
selectedCategories = Categories.Select(m => m.Id).ToList();
}
return selectedCategories;
}
set { selectedCategories = value; }
}
Run Code Online (Sandbox Code Playgroud)
这里有一点点.set属性的方法很简单:当我们返回一个已发布的值时,只需设置selectedCategories为该值即可.这get有点复杂:在这里我们需要压缩你的类别对象列表(Categories这里称为因为我不知道它实际来自哪里)到这些类别的id的简单列表中.
现在,在您看来:
@Html.ListBoxFor(m => m.SelectedCategories, Model.CategoryChoices)
Run Code Online (Sandbox Code Playgroud)
这就是你所需要的.您正在使用ListBox控件,因此它已经是一个多选列表.并且,通过将其绑定到所有当前选定的ID的列表,它知道在SelectListItem其获取的列表中自动选择哪些项目Model.CategoryChoices.
在您的帖子操作中,您需要将这些ID转换为其关联的对象:
var newCategories = repository.Categories.Where(m => carEditViewModel.SelectedCategories.Contains(m.Id));
Run Code Online (Sandbox Code Playgroud)
然后,您可以手动将模型的类别设置为此新列表:
car.Categories = newCategories;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6292 次 |
| 最近记录: |