Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have create AJAX function to get img scr now i want to place it in on page
var imgscr='~/content/1.jpg' //geting from ajax
@Url.Content(imgscr); // i am getting error as imgscr not found

C#
public ActionResult Data()
       {

           List<ImgAudioEntity> listImgAudioEntity = new List<ImgAudioEntity>();
           listImgAudioEntity.Add(
               new ImgAudioEntity { ImgSrc = "Content/SliderImg/1.jpg", AudioSrc = "~/SliderAudio/1.mp3" }


               );

           listImgAudioEntity.Add(new ImgAudioEntity { ImgSrc = "Content/SliderImg/2.jpg", AudioSrc = "~/SliderAudio/2.mp3" });
           listImgAudioEntity.Add(new ImgAudioEntity { ImgSrc = "Content/SliderImg/3.jpg", AudioSrc = "~/SliderAudio/3.mp3" });
           listImgAudioEntity.Add(new ImgAudioEntity { ImgSrc = "Content/SliderImg/4.jpg", AudioSrc = "~/SliderAudio/4.mp3" });
           return Json(listImgAudioEntity,JsonRequestBehavior.AllowGet);
       }

JavaScript
<script>
    var data;
    $(function () {

        var myUrl = '@Url.Action("Data", "Home")';
        $.ajax({
            url: myUrl,
            type: "GET",

            datatype: "JSON",
            contentType: "application/json; charset=utf-8",
            success: function rslt(msg) {
                data = msg;

                SetFrame(data[0].ImgSrc, data[0].AudioSrc);

            }
        });

    });

    function SetFrame(imgscr, audiosrc) {
        console.log(imgscr);
        console.log(audiosrc);


        $('#imgSlider').attr('src', imgscr);
        $('#audiosource').attr('src', audiosrc);
    }
</script>

in console 
Array[4]
0: Object
AudioSrc: "~/SliderAudio/1.mp3"
ImgSrc: "Content/SliderImg/1.jpg"
__proto__: Object
1: Object
2: Object
3: Object
length: 4
__proto__: Array[0]
Posted
Updated 17-Jun-14 22:25pm
v3
Comments
Kornfeld Eliyahu Peter 18-Jun-14 3:21am    
It seems you mixed up client and server code, but it hard to tell. Please expand your code sample with a few more lines around...
Member-515487 18-Jun-14 4:00am    
please check
Kornfeld Eliyahu Peter 18-Jun-14 4:16am    
Can you show the json returns from Data(). Possibly its structure is a bit different from what you think...
Member-515487 18-Jun-14 4:25am    
i am getting proper array and getting src of img

1 solution

U can try @html.Action(imgscr)
 
Share this answer
 
Comments
Member-515487 18-Jun-14 3:44am    
not working
Member-515487 18-Jun-14 3:45am    
giving error name imgscr does not exist in current context

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900