1

this is my Model :

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; }
    }

This is my Controller :

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");
        }

This is my View :

<div class="form-group">
            <label for="Categorie">Categorie : </label>
            @Html.ListBoxFor(model => model.SelectItems, "CAT_ID")
            @Html.ValidationMessageFor(model => model.CAT_ID)
         </div>

And this the error That I got :

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>'

What I'm trying to do is getting all data from Categorie Tabel and put it in ViewBg.CAT_ID. Then In my View I have a listBox filled with Item From ViewBag.CAT_ID, and the selected Value Will be set in SelectItems.

1
  • try my code and let me know. Commented May 14, 2014 at 11:26

1 Answer 1

3

Here is the issue.

@Html.ListBoxFor(model => model.SelectItems, "CAT_ID")

The second argument should be the select list rather than a string, so it should be like

@Html.ListBoxFor(model => model.SelectItems, (ViewBag.CAT_ID as SelectList)) 

OR

@{
 var list = ViewBag.CAT_ID as SelectList;
}

@Html.ListBoxFor(model => model.SelectItems, list ) 
Sign up to request clarification or add additional context in comments.

6 Comments

This Work fine but I'm getting other error now See my update :D
Please change the code as well which you tried so far so that we can figure out the issue.
I fixed the error See my Update on your post. thank you so much.
@Chlebta - I updated the code of Jitendra accordingly. Editing an answer is not necessarily the right tool to communicate with the author - prefer comments instead. However fixing a little typo is fine if it is obvious. I replaced model.CAT_ID by model.SelectItems because it is obvious from your original code.
@Chelbta - ... presuming that model.SelectItems is the property that the user will change by using the listbox. It is a bit confusing because SelectList sounds as if it was the list of available items to select from. The items that the user selected could be better named e.g. as SelectedCategoryIDs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.