getJSON数据函数不适用于IIS

时间:2015-11-03 15:31:52

标签: c# json asp.net-mvc iis razor

我有一对类型和放大器的级联下拉列表子类型。当在本地调试模式下进行测试时,这些工作非常完美,但是一旦我发布到IIS,子类型列表就不会填充并且是空白的。我没有收到任何错误消息。 我在IE9&谷歌Chrome V46也可以得到相同的结果。

控制器是:

    public ViewResult StartRA()
    {
        var user = User.Identity.Name;
        string userName = user.Substring(7);
        var creator = Peopledb.People.FirstOrDefault(x => x.Username == userName);
        var creatorContractId = (int)Session["LoggedContractId"];

        StartRiskAssessmentViewModel viewModel = new StartRiskAssessmentViewModel
        {
            RiskAssessment = new RiskAssessment(),
            CreatorId = creator.PersonId,
            CreatorContractId = creatorContractId,
            Assessor = creator.FirstName + " " + creator.LastName,
            Locations = Peopledb.Locations.Where(x => x.Dormant == false).OrderBy(x => x.LocationName).ToList(),
            Types = db.Types.Where(x => x.Dormant == false).ToList(),
        };
        return View(viewModel);
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult SubTypeList(int typeId)
    {
        var subTypes = db.SubTypes.Where(x => x.Dormant == false && x.TypeId == typeId).ToList();

        if (HttpContext.Request.IsAjaxRequest())
            return Json(new SelectList(
                            subTypes,
                            "SubTypeId",
                            "SubTypeName"), JsonRequestBehavior.AllowGet
                        );

        return View(subTypes);
    }

Razor的观点是:

@model RACentral.ViewModels.StartRiskAssessmentViewModel

@{
ViewBag.Title = "Start RA";
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <h3 class="col-md-offset-2">Type</h3>
    <div class="form-group">
        @Html.LabelFor(model => model.TypeId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.TypeId, new SelectList(Model.Types, "TypeId", "TypeName", 0), "Please Select", new { @class = "form-control", @id = "Types" })
            @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
        </div>
    </div>

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

    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-primary" /> @Html.ActionLink("Cancel", "IndexLoggedIn", "Home", null, new { @Class = "btn btn-danger" })
        </div>
    </div>
</div>
}


@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
            $(function () {
        $("#Types").change(function () {
            $.getJSON('@Url.Action("SubTypeList","RiskAssessment")' {
                var items = "<option>Please Select</option>";
                $.each(data, function (i, subtype) {
                    items += "<option value='" + subtype.Value + "'>" + subtype.Text + "</option>";
                });
                $("#SubTypes").html(items);
            });
        });
</script>
}

1 个答案:

答案 0 :(得分:2)

您可以使用$ .ajax执行此操作,您可能更容易看到正在发生的事情

$(function () {
    $("#Types").change(function () {
        $.ajax({
            type: "get", // can change to post easily
            data: {typeId: $(this).val()}, // or this.value
            url: "@Url.Action("SubTypeList", "RiskAssessment")" // use url helper here
        }).done(function (data) {
            var items = "<option>Please Select</option>";
            $.each(data, function (i, subtype) {
                items += "<option value='" + subtype.Value + "'>" + subtype.Text + "</option>";
            });
            $("#SubTypes").html(items);
        });
    });
});

在你的代码中使用url helper是正确的方向,你只需要包含你的params。

$.getJSON(
    "@Url.Action("SubTypeList", "RiskAssessment")",
    { typeId: $(this).val() })
.done(function (data) {
    var items = "<option>Please Select</option>";
    $.each(data, function (i, subtype) {
        items += "<option value='" + subtype.Value + "'>" + subtype.Text + "</option>";
    });
    $("#SubTypes").html(items);
});