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 fromJStoMVC controller. Take a look here.