如何使用mvc5将javascript对象(url)从一个视图传递到另一个视图

如何使用mvc5将javascript对象(url)从一个视图传递到另一个视图,javascript,c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-5,Javascript,C#,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 5,View1.cshtml: <script> var selectedImage; function select(image) { var images = document.querySelectorAll('img'); var index = 0, length = images.length; for (; index < length;

View1.cshtml:

          <script>
          var selectedImage;
         function select(image) {
            var images = document.querySelectorAll('img');
            var index = 0, length = images.length;
            for (; index < length; index++) {
                images[index].classList.remove('selected');
            }
            selectedImage = image.id;
            image.classList.add('selected');
            var image = document.getElementById(selectedImage);
            var audioId = image.getAttribute('data-audio')
            var audioElement = document.getElementById(audioId);


                // Check browser supporta
                if (typeof (Storage) !== "undefined") {
                    // Store
                    localStorage.setItem("audiourl", audioElement);

                    // Retrieve
                     document.getElementById("result").innerHTML = localStorage.getItem("audiourl");
                }

这是view1这里是javascript中的audiourl,我必须在c函数的view2.cshtml中传递它。我必须将javascript对象从视图1传递到视图2,并将其存储为参数。我上面使用的代码将值存储在本地存储中,但是如何在另一个视图的变量中检索该值,以便我可以将其称为视图2的函数?

您是否尝试在C代码中从本地存储中检索变量?如果是这样,则localStorage是客户端对象,无法在服务器上访问。但是,如果您计划使用javascript,则可以使用localStorage.getItemaudiourl;或localStorage[audiourl]是“View1”和“View2”两个不同控制器操作的视图,或者它们都用于显示单个操作的结果。如果有两个不同的操作,在调用第二个控制器操作时,应在url中提供数据作为参数。@BjörnBoxstart是的,它们是两个不同的操作。你能详细说明你的答案吗?第二个视图将在单击时执行。@JodiSupporter我已经在使用localStorage.getItemaudiourl;但问题是,我必须在另一个视图中定义的c代码中调用此值。在“onClick”中,您必须从本地存储中获取数据,并将其作为参数包含在URL中,第二个操作将导致类似的结果。不要忘记将编码值作为参数包含在内,否则将导致异常。
       <div>
        @foreach (var item in Model)
        {
        <audio id="audio1" src="@Url.Content(item.Letter_Record)"></audio>
     <audio id="audio2" src="@Url.Content(item.Letter_Word_Record)"></audio>

    <img id="img1" data-audio="audio1" src="@Url.Content(item.Letter_Pic)" alt="" onclick="select(this)"  />

    <img id="img2" data-audio="audio2" src="@Url.Content(item.Letter_Word)" alt="" onclick="select(this)" />
           </div>