0

I am working on MVC application , i want to apply one if condition but not working. i am missing something. It says too many characters in character literal

Invalid expression term {

    (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     
        }); 
    })();
Bokambo
  • 4,204
  • 27
  • 79
  • 130
  • What does the resulting HTML actually look like? The issue will hopefully be clear from looking at that. – user94559 Jul 24 '16 at 08:06
  • I guess it depends where the error occurs. If it occurs in the browser, then I'm asking what is the HTML output. (This is the code that runs on your server, and emits HTML that is then interpreted by the browser. What is that HTML?) – user94559 Jul 24 '16 at 08:08
  • If the error instead occurs on the server, then perhaps you can share the exact error message and what line of code it says is the problem? – user94559 Jul 24 '16 at 08:08
  • no it is compile time error.. – Bokambo Jul 24 '16 at 08:09
  • Assign the values of the javascript objects before your function (i.e. `var isValid = @Html.Raw(Json.Encode(Model.IsValidCanvasUser)` –  Jul 24 '16 at 08:10
  • @user662285 Can you share the exact error message and which line it occurs on? – user94559 Jul 24 '16 at 08:10
  • @StephenMuecke: Edited but same error. See my edited script above – Bokambo Jul 24 '16 at 08:15
  • 2
    No, You did not understand. The `var isValid = ...` is the first line of code, then replace the razor `@if()` with `if(isValid) { ..` –  Jul 24 '16 at 08:18
  • @Stephen : Thanks it works... – Bokambo Jul 24 '16 at 08:35

1 Answers1

0

While its possible to swap between razor (server side code) and javascript (client side code) using <text> or @:, for example (refer also these answers for more detail)

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

it is easier to just assign the value of IsValidCanvasUser to a javascript variable and use that in a javascript if block

var isValid = @Html.Raw(Json.Encode(Model.IsValidCanvasUser));
var sr = @Html.Raw(Json.Encode(Model.CanvasJsonData));
if(isValid) {
    Sfdc.canvas(function() {
    ....
}
Community
  • 1
  • 1