lundi 21 août 2017

select property of selectlistitem always false

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

And my model and controllers are like below

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


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.




Aucun commentaire:

Enregistrer un commentaire