gpt4 book ai didi

javascript - 在外部js文件中调用ViewBag、ViewData、TempData

转载 作者:行者123 更新时间:2023-11-30 11:50:24 27 4
gpt4 key购买 nike

在 MVC 应用程序中,我想获取外部 .js 文件中的 ViewBag、ViewData、TempData 值。我不想在 view.cshtml 中编写脚本,所以我向其中添加了外部 js 文件。但我无法在该 js 文件中调用此 ViewBag、ViewData、TempData 值。

谁能告诉我如何调用这些值而不在view.cshtml中编写任何js代码

最佳答案

如果您想避免在 cshtml 中编写任何脚本,只需将隐藏字段设置为您想要传递警报的值即可。 Controller :

TempData["Message"] = "My message text";

查看:

@if(TempData["Message"]) {
<input type="text" id="tempdata" class="hidden"
value="@TempData["Message"]"/>
}

JS:

var tempMessage = document.getElementById('tempdata') ? 
document.getElementById('tempdata').value : null;
if(tempMessage) { alert(tempMessage); };

关于javascript - 在外部js文件中调用ViewBag、ViewData、TempData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640731/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com