Jquery如果条件不适用于函数

时间:2016-07-24 08:05:07

标签: javascript jquery asp.net-mvc

我正在研究MVC应用程序,我想应用一个if条件但不工作。我错过了什么。它在字符文字中说了太多字符

  

无效的表达式术语{

    (function () {
        $(document).ready(function () {
            app.applyDatatable("tblInitPricing", false, [7], [7], 5, "desc", true);
            var sr = @Html.Raw(Json.Encode(Model.CanvasJsonData));
            @if(Model.IsValidCanvasUser)
            { //i am trying to open this curly braces here               
                Sfdc.canvas(function() {
                Sfdc.canvas.client.publish(sr.client, {
                    name: 'mybox.sendVal', payload: { value : 'request created'} });
                }) () ;
            }//i am trying to end this curly braces here     
        }); 
    })();

1 个答案:

答案 0 :(得分:0)

虽然可以使用<text>@:在剃刀(服务器端代码)和javascript(客户端代码)之间进行交换(有关更多详细信息,请参阅these answers)< / p>

app.applyDatatable("tblInitPricing", false, [7], [7], 5, "desc", true);
var sr = @Html.Raw(Json.Encode(Model.CanvasJsonData));
@if(Model.IsValidCanvasUser)
{
    <text>           
        Sfdc.canvas(function() {
        Sfdc.canvas.client.publish(sr.client, {
            name: 'mybox.sendVal', payload: { value : 'request created'} });
        })() ;
    </text>
}
// Razor may report syntax errors, but you can ignore them

IsValidCanvasUser的值分配给javascript变量并在javascript if块中使用

更容易
var isValid = @Html.Raw(Json.Encode(Model.IsValidCanvasUser));
var sr = @Html.Raw(Json.Encode(Model.CanvasJsonData));
if(isValid) {
    Sfdc.canvas(function() {
    ....
}