0

I am coding a MVC 5 internet application and I have a question in regards to a View.

I have a ViewModel variable that holds a decimal value. When the View is displayed, I need to get this decimal value and store it in a javascript function.

Here is my Javascript code:

$(document).ready(function () {

    var valueToStore = 29;
}

I know that I can make an Ajax call to a controller to retrieve and store this value, but can I get and store this value when the View loads, and the ViewModel has this value?

Thanks in advance.

2
  • U can use a hidden field in the view which holds this viewmodel value and take that hidden fields value from javascript then. Commented Feb 12, 2015 at 6:38
  • 1
    var myVariable = '@Model.MyDecimal'; Commented Feb 12, 2015 at 6:43

1 Answer 1

1

Initially, we create a hidden input field, where we store the value we want.

<input type="hidden" id="hdnVal" value=" @Model.YourValue"/>

where YourValue is the decimal you refer to.

Then we read our value like below:

$(document).ready(function () {

    var valueToStore = $("#hdnVal").val();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting the following error: Unexpected token ILLEGAL

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.