Skip to content Skip to sidebar Skip to footer

Cannot Access Properties Of Model In Javascript

I have tried a few suggestions on here however did not seem to work, I cannot seem to access the properties of the model. This is what I have so far var widgetModel = '@Html.Raw(J

Solution 1:

Mixing javascript and Razor requires that you surround your Razor call with any code block

@{ ... } or @if(condition){}, etc.

and putting the code itself in an escaped sequence

@: or the <text> tag.

So, knowing this, you can do something like

@{
        <text>
            var widgetModel = '@Html.Raw(Json.Encode(Model.widgets))';
        </text>
     }

See Mix Razor and Javascript code and Using Razor within JavaScript


Post a Comment for "Cannot Access Properties Of Model In Javascript"