Asp.Net MVC 2下拉显示System.Web.MVC.SelectListItem [英] Asp.Net MVC 2 Dropdown Displaying System.Web.MVC.SelectListItem

查看:284
本文介绍了Asp.Net MVC 2下拉显示System.Web.MVC.SelectListItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含EquipmentIDs的列表的表,并具有维护记录另一个表。

I have a table that contains a list of EquipmentIDs and another table that has maintenance records.

当用户编辑一个维护记录我希望这是一个下拉所有从表中的设备ID的列表。

When the user edits a maintenance record I want there to be a drop down list of all of the equipment IDs from the table.

在下拉列表填充,并将其与输入正确数量的填充,但是他们都说的 System.Web.MVC.SelectListItem ,而不是ID的价值。

The dropdown list populates, and it populates with the correct amount of entries, however they all say System.Web.MVC.SelectListItem instead of the value of the ID.

下面是code生成列表:

Here is the code that generates the list:

public ActionResult Edit(int id)
{
    MaintPerformed maintPerformed = maintPerformedRepository.GetMaintPerformed(id);

    IList<EquipmentID> IDs = equipmentIDRepository.GetEquipmentIDAsList();

    IEnumerable<SelectListItem> selectEquipList =  
        from c in IDs
        select new SelectListItem
        {
            //Selected = (c.EquipID == maintPerformed.EquipID),
            Text = c.EquipID,
            Value = c.Sort.ToString()
        };

    ViewData["EquipIDs"] = new SelectList(selectEquipList, maintPerformed.ID);
    return View(maintPerformed);
}

下面是在.aspx页面中的条目的下拉列表:

Here is the entry in the .aspx page for the Dropdown list:

%: Html.DropDownList("EquipIDs") %>

下面是我如何我产生从表中列表:

Here is how I am generating the list from the table:

public List<EquipmentID> GetEquipmentIDAsList()
    {
        return db.EquipmentIDs.ToList();
    }

看来,一切都与指定的文字例外正常工作将在下拉框中显示。

It appears that everything is working correctly with the exception of assigning the text to be displayed in the drop down box.

我缺少或没有正确思考?

What am I missing or not thinking correctly about?

推荐答案

的SelectList SelectListItem 实际上是相互排斥。你应该使用一个其它。 Etiher通的SelectList 的构造你的原始的数据(标识),或者不使用的SelectList 可言,只是让计算机[EquipIDs] 您的枚举 SelectListItem 。如果用后一种方法去,你将不得不调整你的code,这样您设置在SelectListItem的构造所选择的项目(如你做了,但注释掉)。

SelectList and SelectListItem are actually mutually exclusive. You should be using one or the other. Etiher pass the constructor of SelectList your raw data (IDs) or don't use SelectList at all and just make ViewData["EquipIDs"] your enumerable of SelectListItem. If you go with the latter approach, you will have to tweak your code so that you are setting the selected item in the constructor of SelectListItem (as you had done, but commented out).

或者:

ViewData["EquipIDs"] = new SelectList(IDs, maintPerformed.ID, "EquipID", "Sort");

或者

IEnumerable<SelectListItem> selectEquipList =
    from c in IDs
    select new SelectListItem
    {
        Selected = c.EquipID == maintPerformed.EquipID,
        Text = c.EquipID,
        Value = c.Sort.ToString()
    };

ViewData["EquipIDs"] = selectEquipList;

这篇关于Asp.Net MVC 2下拉显示System.Web.MVC.SelectListItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆