数据未绑定到mvc4中的列表框

时间:2014-04-11 10:50:42

标签: asp.net-mvc-4 listbox

我有列表框

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

这里获取列表数据但没有绑定到列表框(列表项值)

这是我的行动方法

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

}

这是我的模型类

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

从视图返回但不绑定到列表框 你的帮助将被挪用

1 个答案:

答案 0 :(得分:1)

试试这个: @Html.ListBoxFor(m=>m.lista ,Model.lista)并将行SelectList objselectlist = new SelectList(items,"Value");更改为:SelectList objselectlist = new SelectList(items,"Value","Text");