0

I have a view page to show a multi select check box. Here is the code to show that.

@{                                                                                                
    foreach (SelectListItem item in Model.VendorCategoryList)                                                                                                
    {                                                                                                    
        string name = item.Text;                                                                                                    
        bool chkResult = item.Selected;//Model.VendorCategoryList;                                                                                                    
        <tr class="VendorCategoryClassSave">                                                                                                        
            <td style="width: 4%;text-align:right;padding-right:15px;">                                                                                                            
                <label>                                                                                                                
                    @Html.CheckBox("chkVendorCategoryDetailId_" + item.Value, chkResult, new { @class = "chkVendorCategoryDetailClass" })                                                                                                            
                </label>                                                                                                        
            </td>                                                                                                        
            <td style="width: 10%;">                                                                                                            
                <label>@Html.Label(name)</label>                                                                                                        
            </td>                                                                                                    
        </tr>                                                                                                
    }
}

Controller

public ActionResult NewVendor(int custid = 0)
{
    List<SelectListItem> VendorCategory = _commonRepository.VendorCategory
        .AsEnumerable().Where(x => x.status == 1).Select(x => new SelectListItem
        {
            Text = x.catename,
            Value = x.cateid.ToString(),
            Selected=false
        }).ToList();
    foreach(SelectListItem item in VendorCategory)
    {
        item.Selected = setSelectedcategory(Convert.ToInt32(item.Value), custid);
    }
    var model = new HomeViewModel
    {
        VendorCategoryList = new SelectList(VendorCategory, "Value", "Text", "Selected")
    };
    return View("../Masters/View_Vendor", model);
}

Model

public HomeViewModel
{
    public SelectList VendorCategoryList { get; set; }
}

What happens is that even though I set "true" for the Selected attribute in the selectlistitem it always shows as false. See the images below pic1. In the pic1 it says clearly that the selected property is "true". But when I select each item from the model in foreach loop, it says that the selected property is "false". See pic2. Pic2. Can you please help me what I am doing wrong.

This is only happening in the foreach loop. Out side of loop it shows the correct value(true). Thanks in advance.

  • 1
    I'd better use `ViewBag`instead of creating a different view model. You can just send this list to view using `ViewBag.selectList = VendorCategory` and then use this list like `foreach item in (SelectListItem)ViewBag.selectList` – Lab Lab Aug 21 '17 at 11:55
  • 1
    You setting `VendorCategoryList` to a new `IEnumerable` by using `new SelectList()` which wipes out all your `Selected` values (it would need to be just `VendorCategoryList = VendorCategory`). But using `SelectListItem` is not appropriate for this and almost everything else in your code is awful practice. Suggest you refer [this answer](https://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for how to do this correctly –  Aug 21 '17 at 11:59
  • You can create your own view model instead of using select list. This is not place to use select list. – Power Star Aug 21 '17 at 12:01
  • @LabLab I got that by using ViewBag.selectList. Thanks – Baiju Christadima Aug 21 '17 at 12:10
  • Using `ViewBag` is a terrible option! And none of this can ever bind to your model when you submit, and you can never get any validation, etc etc. –  Aug 21 '17 at 12:15

0 Answers0