MVC中的空值多选下拉列表 [英] Null value multiselect dropdown in MVC

查看:77
本文介绍了MVC中的空值多选下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在MVC中我有一个多选下拉列表,我正在填充另一个下拉列表。但是我的代码无法在控制器上获得多选值。



查看

---------- ----

Hi All,
IN MVC i have a multiselect dropdown list on change i am populating another dropdown. But not able to get multiselect values on the controller below is my code.

View
--------------

@Html.DropDownList("Country", ViewData["country"] as List<SelectListItem>, new {@multiple="multiple", style = "width:250px", @class = "dropdown1" })





Ajax调用



Ajax call

<script type="text/javascript">
    $(document).ready(function () {
       
        $("#Country").change(function () {
            
            var abc = $("#Country").val();
            alert(abc);
           

            $("#State").empty();
            $.ajax({               
                type: 'POST',
                url: '@Url.Action("GetStates")', // we are calling json method
                dataType: 'json',
                //data: { id: $("#Country").val() },
                data: { "CountryId": abc },
                cache: false,
                success: function (states) {
                    // states contains the JSON formatted list
                    // of states passed from the controller
                    $.each(states, function (i, state) {

                        alert(state.Value);
                        $("#State").append('<option value="' + state.Value + '">' + state.Text + '</option>');
                    }); // here we are adding option for States
                },
                error: function (ex) {
                    alert('Failed to retrieve states.' + ex);
                }
            });
            return false;
        })
    });
</script>





控制器

---------------- ---------



Controller
-------------------------

public JsonResult GetStates(string CountryId)
       {
          return null;
}





但是我的CountryId为多选择下拉案例的NULL只为正常下拉列表我得到的值,



请帮助



我的尝试:



i已经尝试了相同的上述代码,但总是得到多值下拉案例的空值



But i am getting CountryId as NULL for multi select dropdown case only for normal dropdown i am getting the value,

Please help

What I have tried:

i have tried the above code for the same but always get a null value for multiselect dropdown case

推荐答案

(文档) .ready(function(){
(document).ready(function () {


#Country)。change(function(){

var abc =
("#Country").change(function () { var abc =


#Country)。val();
alert(abc);
("#Country").val(); alert(abc);


这篇关于MVC中的空值多选下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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