English 中文(简体)
与MVC2 DropDownList 仅显示类别名称
原标题:Issue with MVC2 DropDownListFor, only displaying the class-name

良好日子

我对Html有这个问题。 DropDownList 我看不到这一点。 它给我提供了正确的选择数量,如果我对本应提交清单的代码作一个断点的话,那么“SelectItemList”物体包含有正确价值的项目,但实际的超文本是:

<select id="Reason_ReasonId" name="Reason.ReasonId"><option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
<option>System.Web.Mvc.SelectListItem</option>
</select>

该模块载有:

public SelectList ReasonList
{
    get
    {
        SelectList selectList;
        List<SelectListItem> selectItems;

        using (var db = new EscalationDataContext())
        {
            var reasons =
                db.Reasons
                .OrderBy(r => r.Reason1)
                .Select(r => new SelectListItem
                {
                    Value = r.ReasonId.ToString(),
                    Text = r.Reason1
                });

            selectItems = reasons.ToList();

            selectList = new SelectList(selectItems);
        }

        return selectList;
    }
}

控制权人只是造成违约的即时,并设定了违约价值:

    public ActionResult Create()
    {
        EscalationModel model = new EscalationModel
        {
            Reason = new Reason { ReasonId = new Guid("598c28c2-877a-44fa-9834-3241c5ee9355"), Reason1 = "Taken too long" },
            ActionedDate = DateTime.Now
        };

        return View(model);
    } 

最后但并非最不重要的是,认为:

<%: Html.DropDownListFor(m => m.Reason.ReasonId, Model.ReasonList) %>

为什么它这样做? 正如我所说的那样,在实际法典中(我认为),我有正确的价值观,但也是正确的。 就像我一样。 如果能事先得到帮助,将非常感激。

最佳回答

Ok. 看来,你必须具体说明,在选择ListItem中,哪些变量用于“Value”,哪些变量用于“Text”。

selectList = new SelectList(selectItems);

Became。

selectList = new SelectList(selectItems, "Value", "Text");

似乎做了些什么!

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签