MVC2 DropDownListFor 的问题,仅显示类名

发布于 2024-09-16 15:17:32 字数 1818 浏览 2 评论 0原文

美好的一天,

我遇到了 Html.DropDownListFor 的问题,我似乎无法解决。如果我在应该呈现列表的代码中设置一个断点,它会为我提供正确数量的选项,“ SelectItemList”对象包含具有正确值的项目,但它吐出的实际 HTML 如下:

<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) %>

任何想法为什么它的行为是这样的?正如我所说,在实际代码中(在视图中)我有正确的值,但是..它不喜欢我..任何帮助将不胜感激,提前致谢!

Good day,

I have this problem with Html.DropDownListFor which I can't seem to work out.. It gives me the correct number of options, if I do a breakpoint in the code where it is supposed to render the list, the "SelectItemList" object contains the items with correct values, but the actual HTML it spits out is the following:

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

The module containts this:

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

The controller just creates a default instantiation and sets the default value:

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

Last but not least, the view:

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

Any ideas why it behaves like this? As I said, in the actual code (in the view) I have the correct values, but.. It doesn't like me.. Any help would be greatly appreciated, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2024-09-23 15:17:32

好吧..看来你必须指定SelectListItem中的哪个变量用于“值”,哪个变量用于“文本”..

selectList = new SelectList(selectItems);

成为..

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

这似乎已经成功了!

Ok.. It seems you had to specify which variable in SelectListItem was used for "Value" and which was used for "Text"..

selectList = new SelectList(selectItems);

Became..

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

Which seems to have done the trick!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文