我尝试做的是,当下拉列表更改时,我调用 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”时,它都是未定义的。错误在哪里?
谢谢