0

我的jquery是这样的

<script type="text/javascript">$(document).ready(function(){
$("#drpId").change(function(e) {
    firstDDLValue = $("#drpId").val();
    $.post('@Url.Action("GetDataForSecond", "Login")', { fstValue: firstDDLValue }, function (e) {
        var select = $("#secondDDLID");
        select.empty();
        $.each(result, function (index, Data) {
            select.append($('<option/>', {
                value: result.Value,

            }));
        });
    });
});
});

我的控制器代码是

  public JsonResult GetDataForSecond(string fstValue)
    {
        Domain obj = new Domain();
        var data = obj.LoadSubDomain(fstValue); return Json(data, JsonRequestBehavior.AllowGet);

在数据中,我得到了第二个下拉列表的所有值,但考虑到它没有约束力。我的视图代码就是这样

 <div class="editor-field">
        @{
var data = ViewBag.MyData;
            <select name="drpId" id="drpId">
                <option value="yourvalue">Select DomainName </option>
                @foreach (var te in data)
                {
                    <option>@te.DomainName</option>

                }
            </select>
        }
        @Html.ValidationMessageFor(model => model.DomainName)
    </div>
    <div class="editor-field">
        <select name="secondDDLID" id="secondDDLID">
            <option value="yourvalue"></option>
        </select>
    </div>
4

1 回答 1

1

做这个:

在您的视图中添加此脚本:

$("#drpId").change(function () {
        firstDDLValue = $("#drpId").val();
        $.post('@Url.Action("GetDataForSecond", "Login")', { fstValue: firstDDLValue }, function (result) {
            var select = $("#secondDDLID");
            select.empty();
            $.each(result, function (index, Data) {
                select.append($('<option/>', {
                    value: Data.Value,
                    text: Data.Text
                }));
            });
        });
    });

以及在控制器中绑定第二个 ddl 的方法:

 public JsonResult GetDataForSecond(string fstValue)
    {
        Domain obj = new Domain();
        var result = obj.LoadSubDomain(fstValue);
        IList < SelectListItem > Data = new List<SelectListItem>();
        for (int i = 0; i < result.Count; i++)
        {
           Data.Add(new SelectListItem()
            {
                Text = result[i].Name,
                Value = result[i].Name,
            });
        }
        return Json(Data, JsonRequestBehavior.AllowGet);
    }
于 2013-03-22T06:27:09.633 回答