我对我的控制器操作进行了Ajax调用,如下所示:
var m = $.ajax({
mtype: "GET",
url: '@Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false
});
你可以看到我将actionparametter返回给我的控制器并且控制器必须返回true或false,这必须是我的“m”值,但我无法让它工作。有任何想法吗?
控制器代码:
public virtual JsonResult GetBrandForValidation(string actionparameter)
{
var vendorId = _service.GetVendorIdByUsername(GetUserName());
bool k;
var brands = _service.GetBrandsByVendor(vendorId);
var brand = new BrandsViewModel();
brand.BrandName = "Opret ny Brand...";
brands.Add(brand);
foreach (var brandsViewModel in brands)
{
if (brandsViewModel.BrandName == "Intet")
{
brandsViewModel.BrandName = "";
}
}
var list = brands.Select(s => s.BrandName);
if (list.Contains(actionparameter))
{
k = true;
}
else
k = false;
return Json(k,JsonRequestBehavior.AllowGet);
}
全功能代码:
var checkBrands = function(value, colname) {
var m = $.ajax({
mtype: "GET",
url: '@Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false
});
if (m == true)
return [true, ""];
else
return [false, "Brand eksistere ikke"];
};
我是非常新的,非常糟糕的javascript,所以不要很难判断
答案 0 :(得分:0)
ajax调用是用JavaScript编写的吗?如果是这样,mtype应该是type。
答案 1 :(得分:0)
var checkBrands = function(value, colname) {
$.ajax({
mtype: "GET",
url: '@Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false,
success: function(data){
if(data == 'm'){
//do something
}else{
//do something
}
}
});
};
答案 2 :(得分:0)
<强>解决强>
var checkBrands = function (value, colname) {
var m = $.ajax({
mtype: "type",
url: '@Url.Action("GetBrandForValidation")',
async: false,
cache: false,
data: { actionparameter: value }
}).responseText;
if (m == 'true'){
return [true, ""];
}
else return [false, "Brand eksistere ikke"];
};