Html.DropDownList当我指定的名称选定值不起作用 [英] Html.DropDownList selected value does not work when i specify the name

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

问题描述

我想选择当页面呈现一个下拉列表中选择特定的选项。

I am trying to select a specific option from a dropdown list when the page is rendered.

在我的控制器我有这个code:

ViewBag.PropertyId =新的SelectList(db.Properties,属性ID,属性名,id.ToString());

On my Controller I have this code:
ViewBag.PropertyId = new SelectList(db.Properties, "PropertyId", "PropertyName", id.ToString());

在我看来,这是行不通的。所选的选项是第一个选项始终。

@ Html.DropDownList(属性ID(的SelectList)ViewBag.PropertyId)

On my view, This does not work. The selected option is the first option always.
@Html.DropDownList("PropertyId", (SelectList)ViewBag.PropertyId)

这确实对我的工作的看法,正确的选项被选中。但是,因为我需要选择列表中的名称是属性ID,所以它被正确贴我救不了这一点。

@ Html.DropDownList(sadgfsadsaf(的SelectList)ViewBag.PropertyId)

This does work on my view, the correct option is selected. However, I can not save this because I need the name of the select list to be PropertyId, so that it is posted correctly.
@Html.DropDownList("sadgfsadsaf", (SelectList)ViewBag.PropertyId)

可有人请帮助我明白我要在这里干什么?我明白我应该使用一个ViewModel,而不是Viewbag但我只是想先得到这个工作。

Can someone please help me understand what i should be doing here? I understand i should use a viewmodel instead of Viewbag but I just want get this working first.

感谢您

推荐答案

我有同样的问题之前,这是一个名称冲突。试着改变你的下拉菜单和/或值的名称,看看会发生什么。

I've had this same issue before and it was a name collision. Try changing your dropdown and/or value name and see what happens.

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

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