0

我尝试做的是,当下拉列表更改时,我调用 jquery.post() 方法来使用 json 获取图片。这是它的代码:

$.post('@Url.Action("GetImage", "urunler")', { cId: $(this).val(), pId: prd }, function (data) {
                $(".prd-image img").attr("src", data.ImgSmall);

            });

控制器代码:

[HttpPost]
        public ActionResult GetImage(string cId, string pId)
        {
            long productId = long.Parse(pId);
            long colorId = long.Parse(cId);

            var productViewModel = new ProductViewModel();

            productViewModel.PTemp = productTempRepository.Get(x => x.ColorId == colorId && x.ProductId == productId);
            productViewModel.PImage = productImageRepository.GetMany(x => x.TempId == productViewModel.PTemp.Id);

            return Json((from obj in productViewModel.PImage select new { ImgSmall = obj.ImgSmall.Remove(0,1), ImgBig = obj.ImgBig.Remove(0,1) }), JsonRequestBehavior.AllowGet);

        }

但是每当我尝试设置图像 src “data.ImgSmall”时,它都是未定义的。错误在哪里?

谢谢

4

1 回答 1

3

似乎您将列表作为 json 结果返回:

 return Json((from obj in productViewModel.PImage select new { ImgSmall = obj.ImgSmall.Remove(0,1), ImgBig = obj.ImgBig.Remove(0,1) }), JsonRequestBehavior.AllowGet);

所以“数据”将是一个列表......

如果你这样做:

 return Json((from obj in productViewModel.PImage select new { ImgSmall = obj.ImgSmall.Remove(0,1), ImgBig = obj.ImgBig.Remove(0,1) }).First(), JsonRequestBehavior.AllowGet);

然后它应该工作......

于 2012-02-17T14:24:23.123 回答