JavaScript错误:条件编译在MVC2查看关闭 [英] JavaScript Error: conditional compilation is turned off in MVC2 View

查看:133
本文介绍了JavaScript错误:条件编译在MVC2查看关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想呼吁点击JavaScript函数在MVC2查看页面。

 <一的onclick =SelectBenefit(小于(%)= o.ba_Object_id%GT;,<%= o.ba_Object_ code%>)的href = #>选择< / A>

JavaScript函数

 函数SelectBenefit(ID,code){
     警报(ID);
     警报(code);
 }

下面ba_Object_Id和code是从视图模型的值。如果我使用 SelectBenefit(小于(%)= o.ba_Object_id%GT;)以这种方式,其工作的罚款。但是,当我有两个PARAMATERS其not.I正在此错误:

 条件编译已关闭。


解决方案

我认为你需要把周围的第二个参数报价,如果它是一个字符串:

 <一的onclick =SelectBenefit(小于(%)= o.ba_Object_id%>中'<%= o.ba_Object_ code%>')的href =#>选择< / A>

这是说你的参数需要适当EN $ C $的CD,我不会通过他们likse这一点。我将序列它们作为JSON对象,以确保一切正常。像这样的:

 <一的onclick =SelectBenefit(小于%=新的JavaScriptSerializer()序列化(新{ID = o.ba_Object_id,code = o.ba_Object_ code}里)%GT;)的href =#>选择< / A>

然后SelectBenefit功能可能是这样的:

 函数SelectBenefit(利益){
    警报(benefit.id);
    警报(利益code);
}

I am trying to call a JavaScript function on click in a MVC2 View Page.

 <a onclick=" SelectBenefit(<%=o.ba_Object_id %>,<%=o.ba_Object_Code %>)" href="#">Select</a>

JavaScript function

 function SelectBenefit(id,code) {
     alert(id);
     alert(code);
 }

Here ba_Object_Id and Code are the values from the ViewModel. If I use SelectBenefit(<%=o.ba_Object_id %>) in this way, its working fine. But when I have two paramaters its not.I am getting this error:

conditional compilation is turned off.

解决方案

I think that you need to put quotes around the second parameter if it is a string:

<a onclick=" SelectBenefit(<%=o.ba_Object_id %>, '<%=o.ba_Object_Code %>')" href="#">Select</a>

This being said your parameters need to be properly encoded and I wouldn't pass them likse this. I would serialize them as JSON object to ensure that everything is OK. Like this:

<a onclick="SelectBenefit(<%= new JavaScriptSerializer().Serialize(new { id = o.ba_Object_id, code = o.ba_Object_Code }) %>)" href="#">Select</a>

and then the SelectBenefit function might look like this:

function SelectBenefit(benefit) {
    alert(benefit.id);
    alert(benefit.code);
}

这篇关于JavaScript错误:条件编译在MVC2查看关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆