【问题标题】:Javascript compare ViewData list values with DropDownlist Selected ItemJavascript 将 ViewData 列表值与 DropDownlist Selected Item 进行比较
【发布时间】:2011-12-20 14:11:30
【问题描述】:

我已经四处寻找这个问题的答案,但没有什么能完全满足我的要求。 我有一个项目的下拉列表。当用户选择一个项目时,我想将所选值与 2 个不同列表中的项目(均从 db 填充并存储在 ViewData 中)进行比较。这样,我可以根据匹配的列表项填充其他一些表单数据。我想做这个客户端(即,使用 JQuery/Javascript)。 这篇文章看起来是一个好的开始,但我需要将我的 1 值与 2 个不同列表中的项目进行比较: Need simple example of how to populate javascript array from Viewdata list

【问题讨论】:

    标签: jquery asp.net-mvc viewdata


    【解决方案1】:

    终于得到了我的问题的答案。在我的 .cshtml 文件的表单部分中,我有:

    @Html.DropDownList("DropDownCompany", (ViewData["DropDownCompanies"] as SelectList), "Select a Company")
    

    在我的 .cshtml 文件的 javascript 部分中:

     $('#DropDownCompany').change(function () {
    
    var dropdownvalue = $('#DropDownCompany').val();
    // This is a list of objects
    var str2 = @Html.Raw(Json.Encode(ViewData["CompaniesData"]));
    

    然后,我可以稍后(仍在 javascript 部分中)通过以下方式比较它们:

    // CompanyKey is a field of the CompaniesData class
    for(var i in str2) {
    if (str2[i].CompanyKey == dropdownvalue) {
    // Do Stuff
    }                      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多