0

What i try to do is that when a dropdownlist changes i call a jquery.post() method to get the picture using json. Here is the code for it:

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

            });

Controller code:

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

        }

But whenever i try to set the image src "data.ImgSmall" is undefined. Where is the mistake?

Thanks

1 Answer 1

3

It seems to be you return a list as json result:

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

so "data" will be a list...

if you do:

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

Then it should work...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.