Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在c代码中从本地存储器中检索值?_Javascript_C#_Asp.net Mvc_Asp.net Mvc 5_Local Storage - Fatal编程技术网

Javascript 如何在c代码中从本地存储器中检索值?

Javascript 如何在c代码中从本地存储器中检索值?,javascript,c#,asp.net-mvc,asp.net-mvc-5,local-storage,Javascript,C#,Asp.net Mvc,Asp.net Mvc 5,Local Storage,我从一个视图在本地存储中传递一个值,我想在c#函数的另一个视图中调用这个值。两个动作结果在同一控制器中。我不知道如何用c来称呼这个本地存储值,有人能帮我吗 view1.cshtml: <script> ... var audioElement = document.getElementById(audioId); // Check browser supporta if (typeof (Storage) !=

我从一个视图在本地存储中传递一个值,我想在c#函数的另一个视图中调用这个值。两个动作结果在同一控制器中。我不知道如何用c来称呼这个本地存储值,有人能帮我吗

   view1.cshtml:
   <script>
   ...
    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"];
             }
   </script>
 <div id="result"></div>
view1.cshtml:
...
var audioElement=document.getElementById(audioId);
//检查浏览器支持
if(类型(存储)!=“未定义”){
//贮藏
setItem(“audiourl”,audioElement);
//取回
document.getElementById(“结果”).innerHTML=localStorage.getItem[“audiourl”];
}

通过这样做,已经传递了该值,现在如何在c#函数的另一个视图中从
localstorage
接收该值作为参数。

localstorage位于客户端。它未发送到服务器,无法从服务器代码访问


如果您在服务器代码中需要它,您必须自己明确地发送它。

它可能有点长,所以我把它作为一个答案而不是注释。这更多的是一种逻辑,实际的实现取决于您,但分享想法是主要目标

在此之前,让我回忆一下你在这里做什么

<script>
    var audioElement = document.getElementById(audioId);
    if (typeof (Storage) !== "undefined") {
        localStorage.setItem("audiourl", audioElement);
        document.getElementById("result").innerHTML = localStorage.getItem["audiourl"];
    }
</script>
无论何时调用此视图,都要在参数内传递
值。在视图2中,您可以从
ViewBag.StorageItem
的任何部分中检索此值

@Html.HiddenFor(x => x.StorageItem)
如何在
View2
操作方法中设置

您可以从
脚本
控制器
执行此操作。如果您正在通过脚本执行操作,请像这样设置

<script>
    var audioElement = document.getElementById(audioId);
    if (typeof (Storage) !== "undefined") {
        localStorage.setItem("audiourl", audioElement);
        document.getElementById("result").innerHTML = localStorage.getItem["audiourl"];
    }
    // redirecting to view 2..
    window.location.href = "/controller_name/view2?value=" + localStorage.getItem["audiourl"];
</script>
控制器

[HttpPost]
public ActionResult View1(MyModel model)
{
    // ....
    var storageItem = model.StorageItem;
    // ....
    // ....
}
[HttpPost]
public ActionResult View1()
{
    // ....
    var storageItem = Request.Form["StorageItem"];
    // ....
    // ....
}
如果尚未使用
Request.Form[“StorageItem”]
将视图与任何模型绑定,则仍可在控制器中访问该值

除了不存在无参数的
Post
操作之外,如果不使用模型,这只是一个想法


综述:我不知道您使用
localStorage
的目的,但是如果您只是为了将值从一个视图传递到另一个视图,那么您可以忽略这一点,因为您可能已经注意到这一点并不重要,但最终这是您的首选项。

我看到您在
Asp.Net MVC 5
中添加了标记,如果你知道发生了什么,这并不难实现。基本上,您需要将值从
JS
传递到
MVC控制器@Rohit416不,我不想将值从js传递给控制器。实际上我想把值从view1的js传递到view2的c#意味着要把值从一个视图传递到另一个视图。如何显式地发送它。您能详细说明您的答案吗?我没有得到您的答案。您的解决方案正在运行,但window.location.href=“/controller\u name/view2?value=“+localStorage.getItem[“audiourl]”;正在将我重定向到另一个页面,而view2实际上是一个对话框弹出窗口,我正在调用我的view1Oh,现在您提供了您应该在前面提供的详细信息。在这种情况下,您不能使用
window.location.href
,而是需要对
View2
操作和
数据进行ajax调用。很抱歉,现在,你能告诉我如何对View2操作和数据进行ajax调用吗?在这种情况下,你需要更新你的问题,并提供更多详细信息,如如何调用弹出窗口中的视图等。我将我的视图称为这样一个对话框@Html.Partial(“View2”)View2是实际希望从视图1的javascript向其发送值的部分视图(到其c#part。并且c#代码是在视图中而不是在操作结果中编写的)。。。。。使用本地存储是没有必要的,如果不使用本地存储就可以做到这一点,那么就可以了。
<input type = "hidden" name="StorageItem" value="" />

<script>
    var audioElement = document.getElementById(audioId);
    if (typeof (Storage) !== "undefined") {
        localStorage.setItem("audiourl", audioElement);
        var item = localStorage.getItem["audiourl"];

        document.getElementById("result").innerHTML = item;
        document.getElementById("StorageItem").value = item;
    }
</script>
[HttpPost]
public ActionResult View1()
{
    // ....
    var storageItem = Request.Form["StorageItem"];
    // ....
    // ....
}