用于级联下拉列表的MVC $ .getJSON

时间:2014-04-18 15:39:13

标签: asp.net-mvc json

我有一个无效的级联下拉列表。我不确定我在这里缺少什么,因为我看到数据在控制器中返回但是没有加载到子下拉列表控件中。似乎getJSON调用没有收到数据。

控制器代码:

    public ActionResult GetCampaignsForPartner(int partnerKey)
    {

        PartnershipsService svc = new PartnershipsService();
        var oCampaigns = svc.Client.GetCampaigns();
        var oReturn =
               (from c in oCampaigns
                where c.PartnerKey == partnerKey
                select new SelectListItem
                {
                    Text = c.CampaignName,
                    Value = c.CampaignKey.ToString()
                }).ToList();

        return Json(new SelectList(oReturn, "CampaignName", "CampaignKey"), JsonRequestBehavior.AllowGet);
    }

脚本是:

$(document).ready(function () {
    $("#ddlPartners").change(function () {
        var value = $("#ddlPartners option:selected").val();
        $('#ddlCampaigns').empty();

        $.getJSON('@Url.Action("GetCampaignsForPartner")', { partnerKey: value }, function (list) {
            console.log(list);
            $.each(list, function (key, value) {
                var el = $('<option></option>')
                             .attr('value', key)
                             .html(value);
                $('#ddlCampaigns').append(el);
            });
        });
    });
});

HTML是:

<div>
    @Html.LabelFor( x => x.PartnerKey, "Partner:", lbl_class ) @Html.DropDownListFor(x => x.PartnerKey, Model.Partners, new { @id = "ddlPartners", @class = "editor-field" } ) @Html.ValidationMessageFor( x => x.PartnerKey )
</div>
<div>
    @Html.LabelFor( x => x.CampaignKey, "Campaign:", lbl_class ) @Html.DropDownListFor(x => x.CampaignKey, Model.Campaigns, new { @id = "ddlCampaigns", @class = "editor-field" } ) @Html.ValidationMessageFor( x => x.CampaignKey )
</div>

任何帮助您完成这项工作将不胜感激。 谢谢!

2 个答案:

答案 0 :(得分:0)

您可以尝试使用JsonResult而不是ActionResult,我认为它也更有意义。

public JsonResult GetCampaignsForPartner(int partnerKey)
    {
        PartnershipsService svc = new PartnershipsService();
        var oCampaigns = svc.Client.GetCampaigns();
        var oReturn =
               (from c in oCampaigns
                where c.PartnerKey == partnerKey
                select new SelectListItem
                {
                    Text = c.CampaignName,
                    Value = c.CampaignKey.ToString()
                }).ToList();

        return new JsonResult() { Data = new SelectList(oReturn, "CampaignName", "CampaignKey"), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

您可以通过从浏览器调用它来查看它是否首先返回一些值:

http://web/controller/GetCAmpaignsForPartner?partnerKey=1

如果你在那里看到一些数据,那么你的控制器工作正常,你应该检查JS代码,但它看起来也不错。

答案 1 :(得分:0)

(根据需要改变)。在控制器写中,

public JsonResult SellerList(int Id)
        {
            var seller = from s in db.seller
                        where s.CategoryID == Id
                        select s;
            return Json(new SelectList(seller.ToArray(), "SellerID", "SellerName"), JsonRequestBehavior.AllowGet);
        }

在CSHTML中

    <div class="form-group">
        @Html.LabelFor(model => model.CategoryID, "CategoryID", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CategoryID", ViewBag.CategoryID as SelectList, "--Select--", new { id = "CategoryId" })

            @Html.ValidationMessageFor(model => model.CategoryID)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SellerID, "SellerID", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("SellerID", ViewBag.SellerID as SelectList, "--Select--", new { id = "SellerID" })
            @Html.ValidationMessageFor(model => model.SellerID)

        </div>
    </div>

脚本

@Scripts.Render("~/bundles/jquery")
<script type="text/javascript">
    $(function () {
        var items = '<option>Select Seller</Option>';
        $('#StateID').change(function () {

            $.getJSON('/User/DistrictList/' + $('#StateID').val(), function (data) {

                $.each(data, function (i, seller) {
                    items += "<option value='" + seller.Value + "'>" + seller.Text + "</option>";
                });

                $('#DistrictID').html(items);
            });

        });
    });
</script>

脚本中提供的id字段必须与提供给控件的id相同。