5

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.

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. 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 . Commented Nov 11, 2015 at 11:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how to send it explicitly. can you please elaborate your answer ?i'm not getting your answer.
4

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.

5 Comments

your solution is working but window.location.href = "/controller_name/view2?value=" + localStorage.getItem["audiourl"]; is redirecting me to another page and view2 is actually a dialog popup that i m calling on my view1
Oh, now you giving details which you should have given earlier. Well in that case you can-not use window.location.href, instead you need to make an ajax call to View2 action along with data.
sorry for that , now can u please tell me how to to make an ajax call to View2 action along with data.
In that case you need to update your question and provide more details like how are you calling the view inside pop up etc...
I am calling my view as a dialog like this . <dialog id="myDialog2" style="position: absolute; top: 0px; left: 0px; "> @Html.Partial("View2") </dialog> view2 is the partial view to which actually want to send the value(to its c# part. and c# code is written with in the view not in the action result ) from the javascript of view 1..... and using local storage is not necessary if this can be done without using local storage then it will be ok .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.