开发者

Ajax request return value

开发者 https://www.devze.com 2023-04-07 06:14 出处:网络
I have an Ajax call to my controller action looks like this: var m = $.ajax({ mtype: \"GET\", url: \'@Url.Action(\"GetBrandForValidation\")\',

I have an Ajax call to my controller action looks like this:

var m = $.ajax({
        mtype: "GET",
        url: '@Url.Action("GetBrandForValidation")',
        data: { actionparameter: value },
        async: false,
        cache: false
});

As u can see i return actionparametter to my controller an开发者_运维技巧d controller have to return either true or false which has to be my "m" value, but i cant get it to work. Any ideas?

Controller Code:

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

And full function code :

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"];
};

Im quite new and very awfull at javascript, so dont judge hard


Is the ajax call being made in JavaScript? If so, mtype should be type.


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
                 }

            }
});

};


SOLVED

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"];

  };
0

精彩评论

暂无评论...
验证码 换一张
取 消