使用 Jquery 在 ViewBag 中设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11948620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:07:28  来源:igfitidea点击:

Setting up the value in ViewBag using Jquery

jqueryasp.net-mvcviewbag

提问by Napaka Pangit Q Talga

I just want to ask, is there a way to set up the viewbag value dynamically using jquery? I try this code in my script,

我只是想问,有没有办法使用jquery动态设置viewbag值?我在我的脚本中尝试这段代码,

$(".btn").on("click",function(){
@ViewBag.Id = $(this).attr("id")
});

i dont know if its correct but when i try to run my MVC 3 Project, this error appear in my firebug

我不知道它是否正确但是当我尝试运行我的 MVC 3 项目时,这个错误出现在我的萤火虫中

Syntax Error
    = $(this).attr("id")

Please Help. Thanks

请帮忙。谢谢

回答by Flater

You're misunderstanding how the ViewBag works.

您误解了 ViewBag 的工作原理。

When working in MVC, and you open a webpage, this is what (roughly) happens:

在 MVC 中工作时,您打开一个网页,这就是(大致)发生的情况:

  1. The Method 'Index' of FooController is run. At the end, a View is returned.
  2. Your MVC application will then find the view for you, and start rendering it according to the HTML it finds in the related .aspx file. If the program encounters items such as "@ViewBag.Id", it will basically do a string replace with whatever the ".Id" value is. (It's a bit more complicated than that, but for the sake of argument, it basically does a string replace).
  3. After rendering, the HTML document is sent to your browser, who then displays it.
  1. 运行 FooController 的方法“索引”。最后返回一个View。
  2. 然后,您的 MVC 应用程序将为您找到视图,并根据它在相关 .aspx 文件中找到的 HTML 开始呈现它。如果程序遇到诸如“@ViewBag.Id”之类的项目,它基本上会使用“.Id”值进行字符串替换。(它比那要复杂一些,但为了论证,它基本上是一个字符串替换)。
  3. 渲染后,HTML 文档被发送到您的浏览器,然后由浏览器显示。

By the time your browser gets the page, your ViewBag has basically gone 'out of scope'. This is because your ASP (MVC) application uses the ViewBag, but Javascript only has a scope in the web browser document (this is the HTML code that was returned to the the browser bythe application, afterthe ViewBag has gone out of scope. Javascript isn't part of the MVC application, only the resulting webpage.

当您的浏览器获取页面时,您的 ViewBag 基本上已经“超出范围”了。这是因为你的ASP(MVC)应用程序使用ViewBag,但使用Javascript只有web浏览器文档中的范围(这是返回到浏览器的HTML代码应用程序,之后的ViewBag已范围出去了。 Javascript 不是 MVC 应用程序的一部分,只是生成的网页的一部分。

So the short answer is, no you can't do it like that. Try to think of it as doing an inline string replace. You can only put the ViewBag value into the HTML page, not the other way around.

所以简短的回答是,不,你不能那样做。尝试将其视为执行内联字符串替换。您只能将 ViewBag 值放入 HTML 页面,而不能反过来。

Suppose your Id is 5, the following code in the aspx file:

假设你的Id是5,aspx文件中的如下代码:

$(".btn").on("click",function(){
    @ViewBag.Id = $(this).attr("id")
});

Will be sent to the browser as

将发送到浏览器作为

$(".btn").on("click",function(){
    5 = $(this).attr("id")
});

Since your browser only sees this last part, it just doesn't make sense in Javascript. In you case, With the syntax error, it just means your variable hasn't been initialised, and you are trying to access a null.

由于您的浏览器只能看到最后一部分,因此它在 Javascript 中没有意义。在您的情况下,出现语法错误,这仅意味着您的变量尚未初始化,并且您正在尝试访问null.