Html.DropDownList选定值无法正常工作(使用构造与IEnumberable< SelectListItem> [英] Html.DropDownList Selected Value Not Working (Using Constructor with IEnumberable<SelectListItem>

查看:170
本文介绍了Html.DropDownList选定值无法正常工作(使用构造与IEnumberable< SelectListItem>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里所选择的值不为Html.DropDownList辅助方法,工作的问题。见下图:

I have an issue where the selected value is not working for the Html.DropDownList helper method. See below:

这是我的控制器:

public ActionResult Edit(int id = 0)
{
    NewsEvent item = GetItem(id);
    ViewBag.NewsItemId = new SelectList(ViewBag.NewsItemId.Items, "Id", "Name", item.NewsItemId);

    return View(item);
}

这是我的看法:

@Html.DropDownList("NewsItemId",ViewBag.NewsItemId as SelectList, string.Empty,
                           new { @class = "form-control" })

然而,当我尝试在低于鉴于它的工作原理:

However when I try the below in by view it works:

@Html.DropDownList("NewsItemId", string.Empty)

下面还工作,但因为字段名称不匹配模型,它不会正确发布。

The below also works but since the field name does not match the model, it will not post correctly.

@Html.DropDownList("NewsItemIdDrop",ViewBag.NewsItemId as SelectList, string.Empty,
                           new { @class = "form-control" })

我需要使用第一个选项的原因是这样我就可以在类属性添加到控制。

The reason I need to use the first option is so that I can add the class attribute to the control.

有人能帮助我了解我在做什么错了?

Could someone help me understand what I am doing wrong?

推荐答案

您这里有同样的问题:

<一个href=\"http://stackoverflow.com/questions/2278056/dropdownlistfor-not-selecting-value\">DropDownListFor不选择值

问题是你ViewBag属性名称。因为它是相同型号你的财产将无法正常工作。你应该只改变名称的ViewBag道具别的东西,如:

Problem is in your ViewBag property name. Because it is same as your property in Model it will not work. You should just change name of your ViewBag prop to something else, like:

ViewBag.NewsItemList = new SelectList(ViewBag.NewsItemId.Items, "Id", "Name", item.NewsItemId);

和上查看

@Html.DropDownList("NewsItemId",ViewBag.NewsItemList as SelectList, string.Empty,
                           new { @class = "form-control" })

这篇关于Html.DropDownList选定值无法正常工作(使用构造与IEnumberable&LT; SelectListItem&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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