我正在尝试使用mvc中的另一个下拉列表绑定下拉列表中的数据。 当我从类别中选择时,子类别在控制器中的jsonresult操作中返回一个值,但是ajax给了我一般失败的错误,我不知道错误在哪里。
这是控制中的代码
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);
}
});
}
});
});
答案 0 :(得分:0)
尝试一下,
if (Request.IsAjaxRequest())
{
return Json(subCategoryList, JsonRequestBehavior.AllowGet);
}
else
{
return Json("Not Valid Request", JsonRequestBehavior.AllowGet);
}
答案 1 :(得分: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> </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)
<input class="button-primary" type="button" id="btnGetReportFilters" value=" Get Report Filters " 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); ;
}
答案 2 :(得分:0)
当我添加这一行时: - db.Configuration.ProxyCreationEnabled = false;工作得很好为什么家伙,这条线用的是什么?