简体   繁体   English

DropDownListFor SelectList不能识别SelectedValue

[英]DropDownListFor SelectList SelectedValue is not being Recognized

I am working with a simple example of a call to HtmlHelper.DropDownListFor() , and the passed SelectList object has a default value indicated in the constructor: 我正在使用一个简单的示例调用HtmlHelper.DropDownListFor() ,并且传递的SelectList对象在构造函数中具有默认值:

SelectList MySelectList = new SelectList(MyCollection, "ValueField", "TextField", DefaultIdValue);

And then this SelectList object is passed into my view model into a strongly-typed view. 然后,将这个SelectList对象传递到我的视图模型中,成为一个强类型的视图。

If I break on the @Html.DropDownListFor() call, I can see that indeed the SelectList object passed into the DropDownListFor() method invocation has the proper SelectedValue property set to the correct value field value. 如果我在@Html.DropDownListFor()调用上中断,则可以看到确实传递给DropDownListFor()方法调用的SelectList对象已将正确的SelectedValue属性设置为正确的值字段值。 And even through enumerating the select list items, I can see that the correct item has Selected set to true . 即使通过枚举选择列表项,我也可以看到正确的项的Selected设置为true Yet, when this renders this selected value is not actually initially selected, it's still just the first item in the drop down listbox. 但是,当渲染此选定的值时,实际上并没有最初选择它,它仍然只是下拉列表框中的第一项。

What could I be doing wrong here? 我在这里可能做错了什么? I've done all object verification, but this SelectedValue just doesn't seem to be "recognized" here. 我已经完成了所有对象验证,但是在这里似乎无法“识别”SelectedValue Thank you in advance for the assistance! 预先感谢您的协助!

EDIT 编辑

Here is my model (it's a viewmodel that is passed into a strongly-typed view) 这是我的模型(将其传递给强类型视图的视图模型)

public class MyViewModel
{
    public IEnumerable<MyClass> MyClasses { get; set; }
    public SelectList MyClassList { get; set; }

    public int SelectedId { get; set; }
    public int PreSelectedId { get; set; }
}

Then I have logic to determine the PreSelectedId in the action prior to pushing the viewmodel out to the view: 然后,我有逻辑在将视图模型推出视图之前确定操作中的PreSelectedId

        PreSelectedId = // code to get preselected id.  Debugged and working

        SelectList TempList = new SelectList(MyCollectionOfObj, "Id", "TextField", PreSelectedId);

        return View(
            new MyViewModel()
            {
                MyClassList = TempList,
                MyClasses = // retrieves some objects here,
                PreSelectedId = PreSelectedId
            });

The actual selected value visually is the first element. 视觉上 ,实际选择的值是第一个元素。 The actual selected value by breaking on the code and looking at the SelectList.SelectedValue is the correct one (the 5th item, for instance). 通过断开代码并查看SelectList.SelectedValue的实际选择值是正确的值(例如,第五项)。 Visual and implementation are showing two different things, confusingly. 视觉和实现令人困惑地显示了两种不同的事物。

It appears you are trying to bind to property SelectedId , as in 似乎您正在尝试绑定到属性SelectedId ,如

@Html.DropDownListFor(m => m.SelectedId, Model.ClassList)

Since the value of SelectedId is 0 (you don't seem to be setting it anywhere) the helper tries to find an option with a value of 0 , which it cant, so the first option is displayed (it has to show something) which is why you often use the overload of @Html.DropDownListFor() that adds the "--Please select--" label which has a null value. 由于SelectedId值为0 (您似乎没有在任何地方设置它),帮助程序会尝试找到一个值为0的选项,但它无法显示,因此将显示第一个选项(它必须显示一些内容),其中这就是为什么您经常使用@Html.DropDownListFor()的重载来添加带有null值的"--Please select--"标签的原因。

Change you code to 将您的代码更改为

return View(new MyViewModel()
{
  MyClassList = TempList,
  MyClasses = // retrieves some objects here,
  SelectedId = PreSelectedId // change this
});

and remove the 4th argument from the SelectList constructor (its not necessary since the purpose of the strongly typed helpers are to bind your model values) 并从SelectList构造函数中删除第4个参数(这不是必需的,因为强类型辅助函数的目的是绑定模型值)

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

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