0

模型

public class Cart
{
    public int id { get; set; }
    public int userId { get; set; }
    public int  messageId { get; set; }
    public virtual List<Product>  cart { get; set; }
}

视图模型

public class cartVM
{
    public int id { get; set; }
    public int productId { get; set; }
    public int userId { get; set; }
    public int messageId { get; set; }
    public DateTime AddDate { get; set; }
    public virtual List<Product> Product{ get; set; }
    public List<int> SelectedProductIds { get; set; }
    public IEnumerable<SelectListItem> CartChoices { get; set; }
}

填充 Selectedlist 的控制器函数 /// 此处我只能显示 productname 列,如何为 selectlistitem 显示多个列

private void CartChoices(cartVM model)
{
    model.CartChoices = db.Products.Select(m => new SelectListItem
    {
        Value = ((int)m.ProductId).ToString(),
        Text = m.productName
    });
}

Razor View 显示列表框

<div>
    @Html.ListBoxFor(m => m.SelectedProductIds , Model.CartChoices)
</div>
4

0 回答 0