[英]asp.net mvc ListBoxFor - MultiSelectList does not apply the selected item
[英]ASP.net MVC Multiple Select item in ListBoxFor issue
這是我的模特:
public partial class TAUX
{
public short CAT_ID { get; set; }
public int C_GARANT { get; set; }
[Required(ErrorMessage = "Taux est obligatoire")]
public decimal POURC_TAUX { get; set; }
public System.DateTime DATE_EFFET { get; set; }
public int TAUX_ID { get; set; }
public virtual CATEGORIE CATEGORIE { get; set; }
public virtual GARANTIE GARANTIE { get; set; }
public IEnumerable<int> SelectItems { set; get; }
}
這是我的控制器:
public ActionResult Create()
{
ViewBag.CAT_ID = new SelectList(db.CATEGORIE, "CAT_ID", "LIBELLE");
ViewBag.C_GARANT = new SelectList(db.GARANTIE, "C_GARANT", "LIB_ABREGE");
return PartialView("_Create");
}
這是我的觀點:
<div class="form-group">
<label for="Categorie">Categorie : </label>
@Html.ListBoxFor(model => model.SelectItems, "CAT_ID")
@Html.ValidationMessageFor(model => model.CAT_ID)
</div>
這就是我得到的錯誤:
Error 1 :'System.Web.Mvc.HtmlHelper<pfebs0.Models.TAUX>' does not contain a definition for 'ListBoxFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.ListBoxFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)' has some invalid argument
Error 2: Argument 3: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'
我要做的是從Categorie
Tabel獲取所有數據並將其放在ViewBg.CAT_ID
。 然后,在我看來,我有一個listBox
填充產品從ViewBag.CAT_ID
,所選值將設置SelectItems
。
這是問題所在。
@Html.ListBoxFor(model => model.SelectItems, "CAT_ID")
第二個參數應該是選擇列表而不是字符串,所以它應該是
@Html.ListBoxFor(model => model.SelectItems, (ViewBag.CAT_ID as SelectList))
要么
@{
var list = ViewBag.CAT_ID as SelectList;
}
@Html.ListBoxFor(model => model.SelectItems, list )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.