0

I have a pair of cascading drop down lists for Type & Sub Type. These work perfectly when testing in debug mode locally but once I publish to IIS the sub type list doesnt populate and is blank. I dont get any error messages. I have tried this in IE9 & also Google Chrome V46 but get the same result.

The controllers are:

    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);
    }

And the Razor view is:

@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>
}
3
  • Would you add your JSON call? Are you sure the URL in JSON is correct? It could be that the URL has changed, Usually locally you will host your site in the default web site or IIS Express but this is not the case when you go to staging or production. So if you not using Url.Action("controller","action") to get the URL then this most probably is changed. Commented Nov 3, 2015 at 15:35
  • @Moe Ive edited the post to show the use of Url>action instead, however now it doesnt work locally either? Commented Nov 3, 2015 at 15:48
  • did you remove your route config also? Commented Nov 3, 2015 at 15:50

1 Answer 1

2

You can do this using $.ajax and it might be easier for you to see what's happening

$(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);
        });
    });
});

using the url helper in your code was the right direction, you just needed to include your params also.

$.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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.