简体   繁体   English

MVC 3 Dropdown 使用 ViewBag 丢失所选值

[英]MVC 3 Dropdown losing selected value with ViewBag

I am having an issue with dropdowns and the viewbag.我遇到了下拉菜单和 viewbag 的问题。 I am setting a dropdown using the following code in my controller:我在 controller 中使用以下代码设置下拉列表:

        applicationGuid = new Guid(form["applicationList"]);

        var applications = _applicationRepository.List();

        ViewBag.applicationList = new SelectList(applications, "Id", "Name", applicationGuid);

and in the view, this works perfectly and returns the previously selected value (applicationGuid):在视图中,这完美地工作并返回先前选择的值(applicationGuid):

 @Html.DropDownList("applicationList", "")

Now I want to use the following code as I want to add some attributes to the dropdown:现在我想使用以下代码,因为我想向下拉列表中添加一些属性:

 @Html.DropDownList("applicationList", ViewBag.applicationList as SelectList, "", new { rel = "0", @class = "required" }) 

but for some reason, the selected value is not rendered (even though it is passed to the view and I can see selected = "true" against the correct item in the ViewBag SelectList).但由于某种原因,未呈现所选值(即使它被传递给视图,我可以看到 selected = "true" 与 ViewBag SelectList 中的正确项目相对)。

The two examples above render as (this one has the selected="selected"):上面的两个示例呈现为(这个具有 selected="selected"):

<select id="applicationList" name="applicationList"><option value=""></option><option selected="selected" value="2f666645-9b28-406f-bd9f-9ecc009346a6">app1</option><option value="898cbbb5-5dff-4378-b15a-9ecc00b8242f">app2</option></select>

and like so (selected is gone:!):就像这样(选择的已经消失了:!):

<select class="required" id="applicationList" name="applicationList" rel="0"><option value=""></option><option value="2f666645-9b28-406f-bd9f-9ecc009346a6">app1</option><option value="898cbbb5-5dff-4378-b15a-9ecc00b8242f">app2</option></select> 

Can anyone explain what I'm doing wrong here and why it is losing the selected value?谁能解释我在这里做错了什么以及为什么它会丢失所选值? I have found a number of posts that go into how the names of view data items cannot clash etc but I have stripped this right down with random names and nothing seems to work?我发现了一些帖子 go 到视图数据项的名称如何不能冲突等,但我已经用随机名称将其剥离,似乎没有任何效果? Is this a problem in MVC3?这是 MVC3 中的问题吗?

The problem seems to be that when you pass the SelectList as a parameter to Html.DropDownList() , it doesn't like it to have the same name as the actual dropdown list.问题似乎是,当您将SelectList作为参数传递给Html.DropDownList()时,它不喜欢它与实际下拉列表具有相同的名称。

I copied your code and encountered the same problem as you.我复制了你的代码,遇到了和你一样的问题。

But as soon as I changed但是一旦我改变

@Html.DropDownList("applicationList", ViewBag.applicationList as SelectList, "", new { rel = "0", @class = "required" })

to

@Html.DropDownList("applicationListX", ViewBag.applicationList as SelectList, "", new { rel = "0", @class = "required" })

It produced the working output:它产生了工作 output:

<select class="required" id="applicationListX" name="applicationListX" rel="0"><option value=""></option>
<option selected="selected" value="2f666645-9b28-406f-bd9f-9ecc009346a6">app1</option>
<option value="898cbbb5-5dff-4378-b15a-9ecc00b8242f">app2</option>
</select>

I don't know why that's the case, but there's your workaround.我不知道为什么会这样,但有你的解决方法。

After days of hacking around and not wanting to change the name of the Select List (because I didn't want to break autobinding of the model on post back), the alternate solution at the end of this article showed me the way.经过几天的修改并且不想更改 Select 列表的名称(因为我不想在回发时破坏 model 的自动绑定), 本文末尾的替代解决方案向我展示了方法。 Don't use ViewBag.不要使用 ViewBag。 Use ViewData instead, with the same name as the model field's name, and either don't pass anything else to DropDownList, or pass null as the second parameter in case you want the third parameter for HTML attributes (I did).请改用 ViewData,其名称与 model 字段的名称相同,并且不要将任何其他内容传递给 DropDownList,或者将 null 作为第二个参数传递,以防您想要 Z4C4AD5FCA2E381CED0 的第三个参数(属性)。

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

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