2015-11-03 72 views
0

我有一對類型爲&子類型的級聯下拉列表。當在本地進行調試模式下測試時,這些工作是完美的,但是一旦我發佈到IIS,子類型列表不會填充並且爲空。我沒有得到任何錯誤消息。 我已經在IE9 &也試過Google Chrome V46,但得到了同樣的結果。getJSON數據功能不能在IIS上工作

的控制器:

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> 
} 
+0

你會添加你的JSON調用嗎?你確定JSON中的URL是正確的嗎?可能是URL已更改,通常在本地,您將在默認網站或IIS Express中託管您的站點,但當您進行分段或生產時,情況並非如此。所以,如果你不使用Url.Action(「controller」,「action」)來獲取URL,那麼這很可能會改變。 – Moe

+0

@我已編輯帖子以顯示Url> action的用法,但是現在它在本地不起作用? –

+0

你是否也刪除了你的路由配置? – JamieD77

回答

2

可以使用$就做到這一點,讓你看到正在發生的事情可能更容易

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

這就解決了這個問題,非常感謝 –