简体   繁体   English

Html.DropDownList选择的值不起作用(使用带有IEnumerable的构造函数) <SelectListItem> )

[英]Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable<SelectListItem>)

I have an issue where the selected value is not working for the Html.DropDownList helper method. 我有一个问题,其中所选值不适用于Html.DropDownList帮助器方法。 See below: 见下文:

This is My Controller: 这是我的控制器:

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

    return View(item);
}

This is my View: 这是我的观点:

@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. 我需要使用第一个选项的原因是我可以将class属性添加到控件中。

Could someone help me understand what I am doing wrong? 有人能帮助我理解我做错了什么吗?

You have the same problem here: 你有同样的问题:

DropDownListFor Not Selecting Value DropDownListFor不选择值

Problem is in your ViewBag property name. 问题出在您的ViewBag属性名称中。 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道具的名称更改为其他名称,例如:

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

and on View 并在视图上

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

I tried lot of stuff but eventually got it working like this 我尝试了很多东西,但最终让它像这样工作

In controller 在控制器中

int valittu_tila = 1;
var tapahtumantilat = new List<SelectListItem>();

tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa kirjataan", Value = "0" });
tapahtumantilat.Add(new SelectListItem { Text = "Odottaa jonossa", Value = "1"});
tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa käsitellään", Value = "2"});

ViewBag.tilalista = new SelectList(tapahtumantilat, "Value", "Text", valittu_tila);

And then in view I just put (I have the onchange event and class for dropdown too here but you can remove those if you want). 然后在视图中我只是放(我在这里有onchange事件和类下拉,但如果你想要你可以删除它们)。

@Html.DropDownList("tuki_tila", ViewBag.tilalista as SelectList, "-- valitse tila --", new { @onchange = "executeticketsearch();", @class = "haku_dropdowns" })

(Sorry about finnish in the text values and variable names, but those shouldn't matter :D) (抱歉芬兰语中的文本值和变量名称,但那些应该无关紧要:D)

I did not like the fact that you can use 我不喜欢你可以使用的事实

@Html.DropDownList("NewsItemID") 

and it works. 它的工作原理。 Then you need to add the 'class' attribute and suddenly it breaks. 然后你需要添加'class'属性,然后它会突然中断。 I saw one place working and another not working using the syntax 我看到一个地方在工作,另一个地方没有使用语法

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

When I researched this, I determined the ViewBag.NewsItemId was actually a null in the view. 当我研究这个时,我确定ViewBag.NewsItemId在视图中实际上是null。 So a working syntax, with the Model and ViewBag fields having the same name is 因此,一个工作语法,Model和ViewBag字段具有相同的名称

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

First of all, how do you populate ViewBag.NewsItemId.Items ? 首先,如何填充ViewBag.NewsItemId.Items We don't see where the values come from. 我们没有看到价值来自哪里。

In order to make a selected value work, it must be of the same type as the elements of your collections. 为了使选定值有效,它必须与集合的元素具有相同的类型。 If item.NewsItemId is of type T , ViewBag.NewsItemId.Items must be of type IEnumerable<T> (or any other collection type implementing it). 如果item.NewsItemId的类型为T ,则ViewBag.NewsItemId.Items必须是IEnumerable<T>类型(或实现它的任何其他集合类型)。 It does not seem to be the case, because you set NewsItemId as the selected value while your collection is based on NewsItemId.Items which may not be of the same type. 似乎并非如此,因为您将NewsItemId设置为所选值,而您的集合基于可能不是同一类型的NewsItemId.Items

您应该更改DropDownList字段的名称:

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

将viewbag属性名称更改为其他页面上使用的模型变量名称。

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

change to 改成

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

ViewBag.NewsItemIdList name changed. ViewBag.NewsItemIdList名称已更改。 also change in controller. 也改变了控制器。

I had the same problem. 我有同样的问题。 In the example below The variable ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] has a SelectListItem list with a default selected value but such attribute is not reflected visually. 在下面的示例中,变量ViewData [“DATA_ACREDITO_MODELO_INTEGRADO”]具有带有默认选定值的SelectListItem列表,但此类属性未在视觉上反映。

// data 
        var p_estadoAcreditacion = "NO";
        var estadoAcreditacion = new List<SelectListItem>();
        estadoAcreditacion.Add(new SelectListItem { Text = "(SELECCIONE)"    , Value = " "    });
        estadoAcreditacion.Add(new SelectListItem { Text = "SI"              , Value = "SI"   });
        estadoAcreditacion.Add(new SelectListItem { Text = "NO"              , Value = "NO"   });

        if (!string.IsNullOrEmpty(p_estadoAcreditacion))
        {
            estadoAcreditacion.First(x => x.Value == p_estadoAcreditacion.Trim()).Selected = true;
        }
         ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] = estadoAcreditacion;

I solved it by making the first argument of DropdownList, different to the id attribute. 我通过创建DropdownList的第一个参数来解决它,与id属性不同。

// error:
@Html.DropDownList("SELECT__ACREDITO_MODELO_INTEGRADO"
, ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] as List<SelectListItem>
, new
{
id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
...
// solved :
@Html.DropDownList("DROPDOWNLIST_ACREDITO_MODELO_INTEGRADO"
, ViewData["DATA_ACREDITO_MODELO_INTEGRADO"] as List<SelectListItem>
, new
{
id         = "SELECT__ACREDITO_MODELO_INTEGRADO"
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM