本文介绍了如何连接java脚本和mvc对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了AJAX函数来获取img scr现在我想把它放在页面上

var imgscr ='〜/ content / 1.jpg'//来自ajax

@ Url.Content(imgscr); //我收到错误,因为没找到imgscr



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

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




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

推荐答案




这篇关于如何连接java脚本和mvc对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:54