how can I set viewbag value into hidden fiels and access the value from jquery
4 Answers
If I have understood you correctly then you have answered your question yourself - you need to set ViewBag value into hidden field and access it from jQuery.
In View .cshtml file:
<input type="hidden" id="someName" value="@ViewBag["ProbablyAnotherName"]"/>
In JavaScript:
var value = $("#someName").val();
-
1throwing error in cshtml page : Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'' Dec 5, 2022 at 11:23
it is working for me as following
In View .cshtml file:
<input type="hidden" value="@ViewBag.yourValue" id="yourid" />