2017-05-26 89 views
0

我試圖使用mvc中的另一個下拉列表綁定下拉列表中的數據。 當我從類別中選擇子類別在控制器中返回jsonresult操作的值,但ajax給我的錯誤一般失敗,我不知道錯誤在哪裏。我的jQuery ajax函數總是返回錯誤,並且此錯誤消息是(常規失敗)

這是守則Controlle

 public ActionResult Create() 
    { 
     List<sub_categories> subCategoryList = new List<sub_categories>(); 

     // Select List Category 
     var dropdownListCategoryEnr = new SelectList(db.category.ToList(), "cat_id", "cat_name_en"); 
     ViewBag.cat_id = dropdownListCategoryEnr; 

     //Select List Sub category 
     ViewBag.sub_cat_id = new SelectList(subCategoryList, "sub_cat_id", "sub_name_en"); 

     return View(); 
    } 

    [HttpGet] 
    public JsonResult GetSubCategoryById(string categoryId = "") 
    { 
     List<sub_categories> subCategoryList = new List<sub_categories>(); 
     int ID = 0; 
     if(int.TryParse(categoryId, out ID)) 
     { 
      subCategoryList = db.sub_categories.Where(x => x.cat_id.Equals(ID) && x.is_deleted == false).ToList(); 
     } 

     if(Request.IsAjaxRequest()) 
     { 
      return new JsonResult 
      { 
       Data = subCategoryList, 
       JsonRequestBehavior = JsonRequestBehavior.AllowGet 
      }; 
     } 
     else 
     { 
      return new JsonResult 
      { 
       Data = "Not Valid request", 
       JsonRequestBehavior = JsonRequestBehavior.AllowGet 
      }; 
     } 
    } 

這是視圖代碼: -

@Html.DropDownListFor(model => model.cat_id, (SelectList)ViewBag.CategoryAr, WhiteWhaleLanguage.PleaseSelect, new { @style = "padding:0 12px;", @class = "form-control" }) 
@Html.ValidationMessageFor(model => model.cat_id, "", new { @class = "text-danger" }) 

@Html.DropDownListFor(model => model.sub_cat_id, (SelectList)ViewBag.sub_cat_id, WhiteWhaleLanguage.PleaseSelect, new { @style = "padding:0 12px;", @class = "form-control" }) 
@Html.ValidationMessageFor(model => model.sub_cat_id, "", new { @class = "text-danger" }) 

這是jQuery代碼: -

$(document).ready(function() { 
      $("#cat_id").change(function() { 
       // this will call when Country Dropdown select change 
       var categoryId = parseInt($("#cat_id").val()); 
       if (!isNaN(categoryId)) { 
        var ddsub = $("#sub_cat_id"); 
        ddsub.empty(); // this line is for clear all items from State dropdown 
        ddsub.append($("<option></option").val("").html("Select State")); 

        // Here I will call Controller Action via Jquery to load State for selected Country 
        $.ajax({ 
         url: "@Url.Action("GetSubCategoryById", "Products")", 
         type: "GET", 
         data: { categoryId: categoryId }, 
         dataType: "json", 
         success: function (data) { 
          $.each(data, function (i, val) { 
           ddsub.append(
             $("<option></option>").val(val.sub_cat_id).html(val.sub_name_en) 
            ); 
          }); 
         }, 
         error: function (xhr, ajaxOptions, errorThrown) { 
          alert(xhr.responseText); 
         } 
        }); 
       } 
      }); 
     }); 
+1

當您發出AJAX請求時,您應該可以在Chrome開發人員工具(F12)中檢查它,並且可能會顯示錯誤消息。 – Luke

回答

0

這給一個嘗試,

if (Request.IsAjaxRequest()) 
{ 
    return Json(subCategoryList, JsonRequestBehavior.AllowGet); 
} 
else 
{ 
    return Json("Not Valid Request", JsonRequestBehavior.AllowGet); 
} 
+0

這裏沒有任何錯誤subCateogryList給我的價值,但阿賈克斯代碼給我一般故障 –

0

我已經創建了一個類似的例子,其中根據在父級下拉列表中選擇的值填充下拉菜單。請試一試。

<div class="box box-frame" style="padding-bottom: 10px;"> 
     <div class="box-inner" style="padding-left: 0px"> 
      <div class="box-caption">Search Reports</div> 
      <div style="padding: 2px;"> 
       @Html.ValidationSummary() 
      </div> 
      <ul class="piped font-weight-normal"> 
       <li> 
        <b>Company: </b> 

          @Html.DropDownListFor(m => Model.CompanyId, 
                 Model.CompanyLookupValues.Select(
                  c => new SelectListItem() { Value = c.CompanyId.ToString(), Text = c.CompanyName } 
                 ), 
                 "-- select --", 
                 new { @class = "width-4",onchange = "GetReportTypes();" }) 
          @Html.ValidationMessageFor(m => Model.CompanyId) 
          <text>&nbsp;&nbsp;</text> 

        <b> Report Type: </b> 
        @Html.DropDownListFor(m => Model.ReportId, 
           Model.ReportLookupValues.Select(c => new SelectListItem() { Value = c.ReportId.ToString(), Text = c.ReportName }), 
           "-- select --", 
          new { @class = "width-4" }) 
         @Html.ValidationMessageFor(m => Model.ReportId) 
         &nbsp;&nbsp; 
         <input class="button-primary" type="button" id="btnGetReportFilters" value="&nbsp;&nbsp;Get Report Filters&nbsp;&nbsp;" onclick="GetReportFilters();" /> 
       </li> 
      </ul> 

     </div>`enter code here` 
    </div> 

jQuery代碼

 function GetReportTypes() { 
       var companyId = $("#CompanyId").val(); 
       if (companyId != '') { 
        var url = '@Url.Action("GetReportTypes", "Report")'; 
        $.getJSON(url + '?companyId=' + companyId, function (data) { 
         var select = $("#ReportId"); 
         select.empty(); 
         select.append($('<option/>', { 
          value: '', 
          text: "-- select --" 
         })); 

         $.each(data, function (index, itemData) { 
          select.append($('<option/>', { 
           value: itemData.ReportId, 
           text: itemData.ReportName 
          })); 
         }); 
        }); 
       } 
      } 

MVC代碼

public JsonResult GetReportTypes(int companyId) 
     { 
      List<Report> reportTypes = new List<Report>(); 
      if (companyId > 0) 
      { 
       // for admin, return all report types 
       if (IsAdministrator) 
       { 
        reportTypes = ReferenceDataService.GetReportTypes(true).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList(); 
       } 
       else 
       { 

          reportTypes = ReferenceDataService.GetReportTypes(false).Select(r => new Report { ReportId = r.ReportId, ReportName = r.ReportName }).ToList(); 
       } 


      } 

      return Json(reportTypes, JsonRequestBehavior.AllowGet); ; 

     } 
0

當我添加這一行: - db.Configuration.ProxyCreationEnabled = FALSE;工作很好爲什麼傢伙,這條線是用什麼?

相關問題