Vs'12 asp.net C# MVC4 - Int.Appl.Template EF Code First
Here is my very simple Script
<script class="TractsScript">
$('#Add').click(function (e) {
var val = @ViewBag.ForSection;
alert(val);
});
</script>
As per example I am wanting to simply set a variable in my script
or USE a Viewbag.
or Model.
I haven't been able to find an answer in any of the following forums: StckTrace1,StackTraceBetterAnswer
Other Things i have tried:
var model = @Html.Raw(Json.Encode(Model))
alert(model.Sections);
alert(@ViewBag.ForSection);
When you're doing this
You're probably getting a JSON string, and not a JavaScript object.
You need to parse it in to an object:
You can do this way, providing Json or Any other variable:
1) For exemple, in the controller, you can use
Json.NET
to provideJson
to theViewBag
:2) In the
View
, put the script like this at the bottom of the page.What you have should work. It depends on the type of data you are setting i.e. if it's a string value you need to make sure it's in quotes e.g.
If it's an integer you need to parse it as one i.e.
try this method
Thanks
Use single quotation marks (
'
):