[英]missing ) after argument list, When trying to call JS function onclick
Im trying to add data to a JSON object, stored in session.我试图将数据添加到存储在 session 中的 JSON object 中。 I do this with a js function:
我用 js function 做到这一点:
<section class="products">
@foreach (var prod in Model) {
string path = "~/images/" + prod.ImgURL;
<article>
<a href="@Url.Action("InspectProduct","Products", new {productID = prod.Id})">
<img src="@Url.Content(path)" style="max-width: 75%"></a>
<h3><a href="@Url.Action("InspectProduct","Products")">@prod.Name</a></h3>
<h4>@prod.Price,-</h4>
<a class="btn-add" style="cursor: pointer;" onclick="addToJSONObject(@prod.Id, @prod.Name, @prod.Price, @prod.Info)">Tilføj til kurv</a>
</article>
}
</section>
Im using MVC hence the "@" from the Viewbag.我使用 MVC,因此使用 Viewbag 中的“@”。 When i click the "button" it throws: "Uncaught SyntaxError: missing ) after argument list" on almost every products tag.
当我单击“按钮”时,它会在几乎每个产品标签上抛出:“参数列表后未捕获的语法错误:缺少)”。
It worked fine until i started adding more parameters to the function.在我开始向 function 添加更多参数之前,它运行良好。 Any ideas what could be wrong?
有什么想法可能是错的吗? I dont seem to be missing any ")" or escapes.
我似乎没有遗漏任何“)”或转义。
You found a very basic concept called " context switch ".您发现了一个非常基本的概念,称为“上下文切换”。
If you look at this line:如果你看这一行:
"@Url.Action("InspectProduct","Products")"
You'll find the the first "
initiates your html attribute context. It should be ended by another "
.您会发现第一个
"
启动您的 html 属性上下文。它应该由另一个结束"
。 However there are four "
in between serving other purposes. Namely, they start/end a parameter value context.但是,在其他用途之间有四个
"
。即,它们开始/结束参数值上下文。
There is no way for the interpreter to differentiate those "
. Thus, the syntax error.解释器无法区分那些
"
。因此,语法错误。
Please read about the problem and how you would fix it in your programming language.请阅读该问题以及如何用您的编程语言解决该问题。 Sometimes it is escaping (eg
\"
), sometimes it is using different string literals (eg '
).有时它是 escaping (例如
\"
),有时它使用不同的字符串文字(例如'
)。
When using Razorviews in ASP.NET MVC, the @ will allow you to write c# code directly into HTML.在 ASP.NET MVC 中使用 Razorviews 时,@ 将允许您将 c# 代码直接写入 HTML。 The call to the view-bag data in the foreach-loop is done with:
在 foreach 循环中对视图包数据的调用是通过以下方式完成的:
addToJSONObject(@prod.Id, @prod.Name, @prod.Price, @prod.Info)
However, this will not insert " or ' Therefore the correct way to use model data as a parameter (if string is required):但是,这不会插入 " 或 ' 因此使用 model 数据作为参数的正确方法(如果需要字符串):
addToJSONObject('@prod.Id', '@prod.Name', '@prod.Price', '@prod.Info')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.