Html.DropDownList选定值使用ViewBag不工作 [英] Html.DropDownList selected value not working using ViewBag

查看:200
本文介绍了Html.DropDownList选定值使用ViewBag不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,一两个小时阅读的东西在这里,没有成功尝试所有的解决方案后,又发现的,我认为它会救我的命...没有这篇文章

长话短说。

下面是我的看法(所有组合)

<$p$p><$c$c>@Html.DropDownList(\"yearDropDown\",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@ Html.DropDownList(yearDropDownxxx(IEnumerable的&LT; SelectListItem&GT;)ViewBag.yearDropDown)
@ Html.DropDownList(yearDropDown,(小于的SelectList&GT)ViewBag.yearDropDown)
@ Html.DropDownList(yearDropDown)

下面是我的控制器

 公众的ActionResult(INT年)
{
VAR年= INT新[] {2007年,2008年,2009年,2010年,2011年,2012}
                。选择(X =&gt;新建SelectListItem {
                    文本= x.ToString(),
                    值= x.ToString(),
                    选择= X ==一年})是不同的()了ToList()。            years.Insert(0,新SelectListItem {值= NULL,文本=所有年份});
            ViewBag.yearDropDown =新的SelectList(年,值,文本,years.Where(X =&GT; x.Selected).FirstOrDefault());
返回查看();
}

下面是我提供的HTML。选定无处可寻。

 &LT;选择一个id =yearDropDownNAME =yearDropDown&GT;&LT;期权价值=&GT;所有年份和LT; /选项&GT;
&LT;期权价值=2007&GT; 2007&LT; /选项&GT;
&LT;期权价值=2008&GT; 2008&LT; /选项&GT;
&LT;期权价值=2009&GT; 2009年&LT; /选项&GT;
&LT;期权价值=2010&GT; 2010&LT; /选项&GT;
&LT;期权价值=2011&GT; 2011&LT; /选项&GT;
&LT;期权价值=2012&GT; 2012&LT; /选项&GT;
&LT; /选择&GT;

不用说,但我会的,我检查了我的收藏和的SelectList实际上有填充传递给控制器​​选定一年SelectedValue属性。但是,当我在渲染视图,它进入第一个选项。

请,我需要为的DropDownList ,而不是 DropDownListFor 解决方案。我强调这一点,因为我在这里看到其他人询问同样的帮助和一堆人给他们的指示,几乎命令他们,用DropDownListFor。还有一个原因,我需要使用DropDownList的。

解决方案:
看看我自己的答案。
然而,这里是我做了简单的修改。

控制器:

  ViewBag.yearDropDown =岁;

查看:

  @ Html.DropDownList(yearDropDown)


解决方案

该问题也可能是名,在这里看到。

在控制器

  ViewBag.PersonList =新的SelectList(db.Person,ID,姓名,p.PersonId);

在View

  @ Html.DropDownList(PersonList(的SelectList)ViewBag.PersonList)

这是行不通的,你要改名字,所以它是不一样的。

  @ Html.DropDownList(PersonList123(的SelectList)ViewBag.PersonList)

于是改变yearDropDown,它会为你工作。

最好的问候
基督教Lyck。

Well, after a couple hours reading stuff over here, trying unsuccessfully all the solutions, also found this article that i thought it would save my life... nothing.

Long story short.

Here is my View (all combinations)

@Html.DropDownList("yearDropDown",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDownxxx",(IEnumerable<SelectListItem>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown",(<SelectList>)ViewBag.yearDropDown)
@Html.DropDownList("yearDropDown")

Here is my Controller

public ActionResult(int year)
{
var years = new int[] { 2007, 2008, 2009, 2010, 2011, 2012 }
                .Select(x => new SelectListItem { 
                    Text = x.ToString(), 
                    Value = x.ToString(), 
                    Selected=x==year }).Distinct().ToList();

            years.Insert(0, new SelectListItem { Value = null, Text = "ALL YEARS" });
            ViewBag.yearDropDown = new SelectList(years, "Value", "Text", years.Where(x => x.Selected).FirstOrDefault());
return View();
}

Here is my rendered HTML. Selected nowhere to be found.

<select id="yearDropDown" name="yearDropDown"><option value="">ALL YEARS</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
</select>

Needless to mention, but i will, i checked in my Watch and SelectList actually has SelectedValue property populated with the selected year passed to the controller. But when i renders at the view, it goes to the first option.

Please, I need the solution for DropDownList, NOT for DropDownListFor. I am highlighting this because i saw other people here asking for the same help and bunch of people gave them instructions, and almost order them, to use DropDownListFor. There is a reason why i NEED to use DropDownList.

SOLUTION: Look at my own answer. However, here are the simple changes i made.

Controller:

ViewBag.yearDropDown = years;

View:

@Html.DropDownList("yearDropDown")

解决方案

The problem may also be the name, see here.

In the Controller

ViewBag.PersonList= new SelectList(db.Person, "Id", "Name", p.PersonId); 

In the View

@Html.DropDownList("PersonList",(SelectList)ViewBag.PersonList )

This will not work, you have to change the name, so it's not the same.

@Html.DropDownList("PersonList123",(SelectList)ViewBag.PersonList )

So change yearDropDown and it Will Work for you.

Best regards Christian Lyck.

这篇关于Html.DropDownList选定值使用ViewBag不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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