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.
2 Answers
Reset to default 6Local 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.
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 fromJS
toMVC controller
. Take a look here. – Rohit416 Commented Nov 11, 2015 at 11:17