【问题标题】:Get selectedValue dropdownList in controller at runtime运行时在控制器中获取 selectedValue 下拉列表
【发布时间】:2014-12-29 13:21:25
【问题描述】:

我想根据另一个dropdownList的selectedValue来设置一个dropdownList的值。

例如:

ViewBag.Brand = new SelectList(db.Brand, "Libel", "Libel");
ViewBag.IdModel = new SelectList(db.Model.Where(model => model.Brand == ViewBag.Brand.SelectedValue), "IdModel", "Descriptive");

我知道这行不通,但它是为了展示我想要的逻辑。

这是我的观点:

<div class="form-group">
    @Html.LabelFor(model => model.IdModel, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("IdModel", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.IdModel, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Brand, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownList("Brand", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.IdModel, "", new { @class = "text-danger" })
    </div>
</div>

他们有什么简单的方法吗?

谢谢!

【问题讨论】:

    标签: asp.net-mvc runtime html.dropdownlistfor selectedvalue


    【解决方案1】:

    在您的情况下,您在更改品牌下拉框时需要进行 ajax 调用或服务器调用。 请参阅下面的示例,它只是使用静态数据来识别品牌模型何时更改,然后使用 jquery(客户端)自动更改模型下拉列表。

    查看端:-

    @*Display the elements in the page*@
    @*------------------------------------------------*@
    <script type="text/javascript" 
          src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <div class="form-group">
      <div class="col-md-10">  
         @Html.DropDownList("Brand", (SelectList)ViewBag.Brand, new { htmlAttributes = new { @class =
             "form-control" } })
      </div>
    </div>
    <div class="form-group">
      <div class="col-md-10">
          @Html.DropDownList("IdModel", (SelectList)ViewBag.IdModel, new {htmlAttributes = new { 
            @class = "form-control" } })
      </div>
    </div>
    @*------------------------------------------------*@
    
    
    @*Script for call the server side function when you changed brand combo from jquery*@
    @*------------------------------------------------*@
    <script type="text/javascript">
      $(function () {
          $("#Brand").change(function () {
            var selectedItem = $(this).val();
             $.ajax({
                 cache: false,
                 type: "GET",
                 url: "/Home/GetModelFromBrand", // User your action and controller name 
                 data: { "Brandid": selectedItem },
                 success: function (data) {
                     $("#IdModel").empty();
                        $.each(data, function (id, option) {
                            $("#IdModel").append($('<option>
                                  </option>').val(option.IdModel).html(option.Descriptive));
                        });
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('Failed to retrieve states.');
                    }
                });
            });
         });
     </script> 
     @*------------------------------------------------*@
    

    控制器端:-

    //Get Method to load the view
    //==============================================================
    public ActionResult Index()
    {
        var brands = new SelectList(new[]
                                  {
                                      new {Libel="Brand1",},
                                      new {Libel="Brand2",},  
                                  },
                                 "Libel", "Libel");
    
        //=======Suppose Brand1 have idmodels1 , idmodels2 models 
        var idmodels = new SelectList(new[]
                                  {
                                      new {IdModel="1",Descriptive="idmodels1",},
                                      new {IdModel="2",Descriptive="idmodels2",},
    
                                  },
                                  "IdModel", "Descriptive");
        ViewBag.Brand = brands;
        ViewBag.IdModel = idmodels;
        return View();
    }
    //==============================================================
    
    
    //Get Action when changed brand combo
    //==============================================================
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetModelFromBrand(string Brandid)
    {
       var obj = new[] {
                                new {IdModel = 3,
                                                     Descriptive = "idmodels3"},
                                new {IdModel = 4,
                                                     Descriptive = "idmodels4"},
                                new {IdModel = 5,
                                                     Descriptive = "idmodels5"}
                            };
    
    
        return Json(obj, JsonRequestBehavior.AllowGet);
    }
    //==============================================================
    

    【讨论】:

      【解决方案2】:

      有两种方式:

      1 在客户端执行; 2 在服务器上做

      1. 在客户端。第二个下拉选项应该包含带有品牌 id 的 id,然后你可以编写 javascript,它应该根据第一个下拉列表的选定值更改第二个下拉列表的选定索引。这里需要根据你的模型结构。

      2. 在服务器上。对第一个下拉菜单的每次更改进行回发。并根据第一个下拉列表中的发布值返回每次新模型进行第二个下拉列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        相关资源
        最近更新 更多