Html.DropDownList Selected Value Not Working (Usin

2019-04-03 17:34发布

I have an issue where the selected value is not working for the Html.DropDownList helper method. 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.

Could someone help me understand what I am doing wrong?

7条回答
贼婆χ
2楼-- · 2019-04-03 18:11
@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. also change in controller.

查看更多
甜甜的少女心
3楼-- · 2019-04-03 18:17

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

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

查看更多
迷人小祖宗
4楼-- · 2019-04-03 18:22

change viewbag property name to other then model variable name used on page.

查看更多
倾城 Initia
5楼-- · 2019-04-03 18:23

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. 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. So a working syntax, with the Model and ViewBag fields having the same name is

@Html.DropDownList("NewsItemId", null, string.Empty,
                       new { @class = "form-control" })
查看更多
家丑人穷心不美
6楼-- · 2019-04-03 18:23

You should change the very name of the field of your DropDownList:

@Html.DropDownList("Some_Name_that_does_not_exist_in_model", ViewBag.NewsItemId as SelectList, string.Empty, new { @class = "form-control" })
查看更多
Explosion°爆炸
7楼-- · 2019-04-03 18:33

You have the same problem here:

DropDownListFor Not Selecting Value

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

and on View

@Html.DropDownList("NewsItemId",ViewBag.NewsItemList as SelectList, string.Empty,
                           new { @class = "form-control" })
查看更多
登录 后发表回答