最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve a value from the local storage in c# code? - Stack Overflow

programmeradmin1浏览0评论

I'm passing a value in local storage from one view and i want to call this value in another view in c# function. Both action results are in same controller. I don't know how to call this local storage value in c# can any one help me ?

   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>

By doing this value has been passed, now how to receive this value from localstorage in another view in c# function as a parameter.

I'm passing a value in local storage from one view and i want to call this value in another view in c# function. Both action results are in same controller. I don't know how to call this local storage value in c# can any one help me ?

   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>

By doing this value has been passed, now how to receive this value from localstorage in another view in c# function as a parameter.

Share Improve this question edited Nov 11, 2015 at 11:08 Rajeshkumar Kandhasamy 6,4617 gold badges60 silver badges83 bronze badges asked Nov 11, 2015 at 11:00 daniadania 871 gold badge2 silver badges8 bronze badges 2
  • I see you have tagged in Asp.Net MVC 5, this is not much difficult to achieve if you know whats going on. Basically what you want is passing a value from JS to MVC controller. Take a look here. – Rohit416 Commented Nov 11, 2015 at 11:17
  • @Rohit416 no i dont want to pass the value from js to controller. actually i want to pass the value from js of view1 to the c# of view2 means want to pass the value from one view to another view . – dania Commented Nov 11, 2015 at 11:37
Add a comment  | 

2 Answers 2

Reset to default 6

Local storege is on the client-side. It isn't sent to the server, and cannot be accessed from server code.

If you need it in your server code, you'll have to explicitly send it yourself.

The actual implementation is up to you; I am sharing the idea.

Your View2 controller should look like this:

public ActionResult View2(string value)
{
    // ......
    ViewBag.StorageItem = value;
    return View();
}

Supply the value to this controller whenever you call it. Inside View2, you can retrieve this value in any section by using ViewBag.StorageItem.

Note: A ViewBag is a dynamic property in ASP.NET MVC and it does not require type conversion and convert type dynamically.

Setting the value in the View2 action method

You can set it either from JavaScript or the controller.

Using JavaScript

var audioElement = document.getElementById(audioId);
if (typeof (Storage) !== "undefined") {
    localStorage.setItem("audiourl", audioElement);
    document.getElementById("result").innerHTML = localStorage.getItem["audiourl"];
}
// redirecting to View2..
window.location.href = "/controller/view2?value=" + localStorage.getItem["audiourl"];

Using controller

You cannot use ViewBag in this case if you want to pass the value around from one action method to another, or from a View to a controller. You need a view model property for this purpose that you can use to post data back to the controller of View1 which can then relay this value to the controller of View2.

For instance: If you have a property in your view model named StorageItem then you can bind it in the view in a hidden field.

HTML

@Html.HiddenFor(x => x.StorageItem)

Controller

[HttpPost]
public ActionResult View1(MyModel model)
{
    // Do something with the value
    var storageItem = model.StorageItem;
    // ....
}

Cannot use a view model?

That would be ideal though, but just in case you cannot then you can still access the value in the controller using Request.Form["StorageItem"] as follows.

HTML

<input type = "hidden" id="StorageItem" name="StorageItem" value="" />

JavaScript

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

Controller

[HttpPost]
public ActionResult View1(Something something)
{
    // ....
    var storageItem = Request.Form["StorageItem"];
    // ....
    // ....
}

Tip: If you are using localStorage just for passing the value around then it is insignificant in ASP.NET MVC. There are better options available.

发布评论

评论列表(0)

  1. 暂无评论