Asp.net 在SelectListBox中预选多个项目

Asp.net 在SelectListBox中预选多个项目,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,正如标题所述,我想在视图加载时预选项目。我有以下代码: @{ var categoryList = new List<SelectListItem>(); foreach (var category in ViewBag.Categories) { categoryList.Add(new SelectListItem { Value = category.CategoryId.ToString(),

正如标题所述,我想在视图加载时预选项目。我有以下代码:

@{
    var categoryList = new List<SelectListItem>();
    foreach (var category in ViewBag.Categories)
    {
        categoryList.Add(new SelectListItem
        {
            Value = category.CategoryId.ToString(),
            Text = category.Name,
            Selected = category.Assigned
        });
    }
}
@Html.ListBox("Categories", categoryList, new { @class = "select-toggle", size = 5 })

但是,当我加载页面时,它不会预先选择项目。我尝试了几种可能的解决方案,如不同的格式和从模型中选择类别,但它们在我的情况下似乎不起作用。有什么想法吗?

我个人更喜欢使用视图模型在视图和操作方法之间传输数据(加载数据以查看并将发布的表单值传递回操作方法)。因此,让我们通过创建视图模型来解决这个问题

public class CreateProductVm
{
   public int ID { set;get;}
   public string Name { set;get;}
   public List<SelectListItem> Categories { set;get;}
   public int[] SelectedCategories { set;get;}
   //Add other properties as needed by the view
}
现在,您的视图将被强类型化为我们的视图模型

@model CreateProductVm
@using(Html.BeginForm())
{
  @Html.TextBoxFor(s=>s.Name)
  @Html.HiddenFor(g=>g.Id)
  @Html.ListBoxFor(x=>x.SelectedCategories ,Model.Categories)
  <input type="submit" />
}

我试过以下几点。而且似乎效果不错

@{
var categoryList = new List<SelectListItem>()
                   {
                       new SelectListItem()
                       {
                            Value = "1",
                            Text = "One",
                            Selected = true
                       },
                        new SelectListItem()
                       {
                            Value = "2",
                            Text = "Two",
                            Selected = false
                       },
                         new SelectListItem()
                       {
                            Value = "3",
                            Text = "Three",
                            Selected = true
                       }
                   };

}
@Html.ListBox("Categories", categoryList, new { @class = "select-toggle", size = 5 })
@{
var categoryList=新列表()
{
新建SelectListItem()
{
Value=“1”,
Text=“一”,
所选=真
},
新建SelectListItem()
{
Value=“2”,
Text=“两个”,
所选=错误
},
新建SelectListItem()
{
Value=“3”,
Text=“三”,
所选=真
}
};
}
@ListBox(“Categories”,categoryList,new{@class=“select toggle”,size=5})

你确定你的产出吗?也许可以试试这个例子。

谢谢你的回答。我收到一个错误,说明我需要返回产品对象。我不需要对Create方法进行预选,而是对Edit方法进行预选。在创建时,用户选择类别;在编辑时,用户选择的类别和未选择的类别必须可见。当然,它必须是可编辑的。我是ViewModel的新手你从哪里得到这个错误的?在我的回答中,您没有在任何地方使用产品对象。您唯一需要使用它的地方是在HttpPost操作中,在这里您将读取模型的属性并将其分配给您的实体对象(产品)。我正在ProductController中工作,当我需要编辑产品时,它需要在selectlist中选择所有当前类别。产品有很多类别,一个类别可以有很多产品(多对多)。那又怎样?selectedcategories属性是一种数组类型,它应该适用于多个类别是的,它只是用于提供信息。当我尝试在ProductsController的编辑函数中返回viewmodel时,它给出了一个错误:Webshop.ViewModels.CreateProductVm不可分配给模型类型“Webshop.Models.Product”
@model CreateProductVm
@using(Html.BeginForm())
{
  @Html.TextBoxFor(s=>s.Name)
  @Html.HiddenFor(g=>g.Id)
  @Html.ListBoxFor(x=>x.SelectedCategories ,Model.Categories)
  <input type="submit" />
}
[HttpPost]
public ActionResult Edit(CreateProductVm model)
{
  //check for model.SelectedCategories;
  var p =db.Products.FirstOrDefault(s=>s.Id==model.Id);
  if(p!=null)
  {
    p.Name = model.Name;
   //Update other properties as needed
   db.SaveChanges();
   return RedirectToAction("Index");
  }
  // to do : return something
}
@{
var categoryList = new List<SelectListItem>()
                   {
                       new SelectListItem()
                       {
                            Value = "1",
                            Text = "One",
                            Selected = true
                       },
                        new SelectListItem()
                       {
                            Value = "2",
                            Text = "Two",
                            Selected = false
                       },
                         new SelectListItem()
                       {
                            Value = "3",
                            Text = "Three",
                            Selected = true
                       }
                   };

}
@Html.ListBox("Categories", categoryList, new { @class = "select-toggle", size = 5 })