DropDownListFor不设置选定值 [英] DropDownListFor does not set selected value

查看:165
本文介绍了DropDownListFor不设置选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示使用HTML帮助DropDownListFor下拉列表视图

I have a view that is displaying a Drop down list using the HTML helper DropDownListFor

<%: Html.DropDownListFor(Function(model) model.Manufacturer, New SelectList(Model.ManufacturerList, Model.Manufacturer))%>

我的控制器通过查看包含制造商和ManufacturerList

My controller is passing the View a ViewModel containing the Manufacturer and the ManufacturerList

Function Search(ByVal itemSrch As ItemSearchViewModel) As ActionResult

     'some code mapping and model code'

     Return View(itemSrch)

End Function

我的视图模型没有什么疯狂的只是填补了ManufacturerList与字符串值的列表,然后制造商属性只是一个包含从下拉列表中选择的值的字符串值。

My ViewModel is nothing crazy just fills the ManufacturerList with a list of string values and then the Manufacturer property is just a string value containing the selected value from the drop down list.

Public Property Manufacturer As String

Public Property ManufacturerList() As List(Of String)

我有这样的观点问题上的下拉列表中设置所选的值,如果我们重新加载搜索查看。当它进入搜索功能和制造商填充了正确的选择的价值和成功通过该值回搜索查看我检查视图模型(ItemSearchViewModel)。在某些时候传递给视图中的数据似乎没有填充选择的值,想知道是否有人对为什么会发生什么,我能做些什么来解决这个问题的一些想法。

I'm having an issue with the view setting the selected value on the drop down list if we are reloading the Search View. I've checked the View Model (ItemSearchViewModel) when it comes into the Search function and the Manufacturer is populated with the proper selected value and successfully passes that value back to the Search View. At some point the data passed to the view doesn't seem to populate the selected value, was wondering if anyone had some ideas on why this is happening and what I can do to fix it.

感谢

推荐答案

没有得到太多关于这个答案,所以开始钻研我怎么能解决这个有点轻松。此外,在我的研究,这似乎是许多人的DropDownListFor HTML辅助的通病。我想必须有一个办法,因为我知道选定的值属性从我的ViewModel实际上获得通过,以查看得到这个工作。所以的JavaScript和jQuery相救,我结束了试图设置使用jQuery .ready函数值选择并能够成功地得到这个工作。因此,这里是我的jQuery的修复:

Didn't get much for answers on this so started digging into how I could fix this somewhat easily. Also in my research this seemed to be a common problem for many people with the DropDownListFor HTML Helper. I figured there had to be a way to get this working since I knew the selected value property from my ViewModel was actually getting passed to the View. So javascript and jQuery to the rescue, I ended up trying to set the selected value using the jQuery .ready function and was able to successfully get this to work. So here's my jQuery fix:

$(document).ready(function() {

    $("#Manufacturer").val("<%: Model.Manufacturer %>");

});

有关使这个容易跟随我用满。就绪语法,或者你可以使用$(函数(){'code'})的缘故;

For sake of making this easy to follow I used the full .ready syntax, or you can just use $(function () { 'code' });

如果你想解决这个问题,而不使用jQuery你可以只使用基本的JavaScript语法为好,它会是这个样子:

If you want to solve this without using jQuery you can just use basic javascript syntax as well, it'll look something like this:

document.getElementByID("Manufacturer").Items.FindByValue("<%: Model.Manufacturer %>").Selected = true;

如果您在使用普通的javascript调用确保当页面加载完成数据到DropDownList的调用此。

If you using the plain javascript call make sure to call this when the page is done loading data to the dropdownlist.

在这两种情况下这一切的code正在做的是设置在下拉列表中的客户端从模型制造商的价值传递选择的值。

In either case all this code is doing is setting the selected value on the drop down list on the client side from the passed in Manufacturer value from the Model.

如果您有任何其他的方式来解决这个问题,请让我知道,但是这是为我工作,希望它会帮助别人与他们的问题。

If you have any other ways to solve this problem please let me know, but this is working for me and hopefully it'll help someone else with their problem.

感谢,

这篇关于DropDownListFor不设置选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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