user8511293
user8511293

Reputation:

Passing Viewbag value to javascript function

I am new to MVC and trying from last 7 hours to pass value from viewBag to javascript function in MVC 5. I have a string variable

ViewBag.c1 = "0.30"

and i passed it to view and stored it as a hidden element

@Html.Hidden("Superman", (string)ViewBag.c1)

and then i want to access the value of this hidden element in following js function

function getValue1() {
return parseFloat(document.getElementById("Superman"));
}

Upvotes: 0

Views: 7433

Answers (3)

Mohsin Mehmood
Mohsin Mehmood

Reputation: 4246

var c1Value = "@Html.Raw(ViewBag.c1)";

function getValue1() {
  return parseFloat(document.getElementById(c1Value).value);
}

Upvotes: 1

solrac
solrac

Reputation: 629

@Html.Hidden("Superman", (string)ViewBag.c1)


function getValue1() {
  return parseFloat($("#Superman").val());
}

//or

function getValue1() {
  return parseFloat(document.getElementById("Superman").value);
}

Upvotes: 4

Ravikumar
Ravikumar

Reputation: 625

You could simply store view-bag value in JavaScript variable like below.

<script> var yourValue = '@ViewBag.c1' </script>

Upvotes: 0

Related Questions