js脚本调用@viewbag的值需要使用以下代码:
<script type="text/javascript">
//var id=@ViewBag.id //这样直接写ViewBag.id会影响下面那条var name语句,因为ViewBag.id后面不能带分号
//var name = "张三"
//所以将ViewBag传值到js中应该这么写!
var id = "@ViewBag.id"//或者var id='@ViewBag.id'
var name="张三"
</script>
一、MVC ViewBag C#传值给javascript
C# using Newtonsoft.Json
public string getRaw() {
return JsonConvert.SerializeObject(iChartList)
}
javascript:
<script type="text/javascript">
try {
var data = JSON.parse('@Html.Raw(@ViewBag.model.getRaw())')
} catch (e) {
dataString = null
console.error(e)
}
new rp_prefer_controller(data)
</script>
二、js 读取viewbag list
@{
foreach (var item in ViewBag.Items)
{
<text>
……
</text>
}
}
string s = ""foreach (var item in ViewData.Keys)
{
s = ViewData[item].ToString() + ","
}
s = s.Substring(0, s.LastIndexOf(','))
ViewBag实际上也是存在ViewData里面的
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)