Karthik
Karthik

Reputation: 207

Data not binding to listbox in mvc4

I have listbox

 @Html.ListBox("lais", new SelectList(Model.lista, "Value", "Text"), new {@class = "mylistbox"});

Here am getting list data but not binding to listbox (list items value )

This is my action method

 public ActionResult PrintRFIDTag()
    {
        Print p =new Print();
        p.lista = GetList();

        return View(p);

    }

    public SelectList GetList()
    {
        System.Management.ManagementScope objMS =
            new System.Management.ManagementScope(ManagementPath.DefaultPath);
        objMS.Connect();
        List<SelectListItem> items = new List<SelectListItem>();
        SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
        ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
        System.Management.ManagementObjectCollection objMOC = objMOS.Get();
        foreach (ManagementObject Printers in objMOC)
        {
            if (Convert.ToBoolean(Printers["Network"]))     // ALL NETWORK PRINTERS.
            {
                var emptyItem = new SelectListItem()
                {
                    Value = Printers["Name"].ToString(),
                    Text = "00"
                };

                items.Add(emptyItem);

            }
        }
        SelectList objselectlist = new SelectList(items,"Value");
        return objselectlist;
    }

}

Here is my model class

  public class Print
{
    public SelectList lista { get; set; }
    public string Name { get; set; }
}

Returning from view but not binding to listbox Your help will be appropriated

Upvotes: 0

Views: 408

Answers (1)

user3383479
user3383479

Reputation:

try this: @Html.ListBoxFor(m=>m.lista ,Model.lista) and change line SelectList objselectlist = new SelectList(items,"Value"); to this: SelectList objselectlist = new SelectList(items,"Value","Text");

Upvotes: 1

Related Questions