我有以下代码:
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using WebShopGoal.Models;
namespace WebShopGoal.ViewModel
{
public class ProductEditViewModel
{
public Products Product { get; set; }
public SelectList SupplierIdList { get; set; }
public MultiSelectList CategoryIdList { get; set; }
public int[] SelectedCategories { get; set; }
public int? SupplierId { get; set; }
private WebshopDBEntities _db;
public ProductEditViewModel()
{
_db = new WebshopDBEntities();
SupplierIdList = new SelectList(_db.Suppliers, "Id", "Name");
CategoryIdList = new MultiSelectList(_db.Categories, "Id", "CName");
}
public void Load(int id)
{
Product = _db.Products.Find(id);
SupplierId = Product.SupplierId;
SelectedCategories = Product.CategoryId.Select(p => p.Id).ToArray();
}
,在最后一位上,它给出了错误。我在不同的ViewModel中使用了相同的代码,但是它在那个模型中工作。 我认为我使用
<div class="form-group">
@Html.LabelFor(model => model.Product.Categories, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(model => model.SelectedCategories, Model.CategoryIdList, htmlAttributes: new { @class = "form-control" })
</div>
</div>
有人可以帮助我吗?有人在这里搜索说使用System.Linq可以解决它,但是我已经知道了
答案 0 :(得分:1)
我认为,您要在这里做的是:
Unable to establish a connection to the database server at localhost:3750 as it does not appear to be running
答案 1 :(得分:0)
看起来像我想要的那样:
SelectedCategories = new int[] { Product.CategoryId };