How do I access a strongly typed view of a model inside javascript?

I noticed that if I start doing this:

var companyID = <% %>;

      

I am not getting intellisense allowing me to access my Model.object, which is supposed to be available to me since it is strongly typed.

What am I missing here?

+2


source to share


3 answers


The intellisense support for javascript is a little sketchy as it is. Make sure you set it up correctly. I just used this tutorial and this at least it gives javascript intellisense.

Now, to test your question, I'll just put the following on one of my pages:



    <script type="text/javascript">
    var test = <%= Model.Site.SiteID %>;
    document.write(test);
</script>

      

and he actually wrote the SiteID. no intellisense for the model, but it does work. hope this helps some.

+1


source


Nothing fancy, it's just syntax mixing that the IDE kicks out as you are in a javascript context in an IDE that tries to accommodate the javascript syntax. It probably resets most of the intellisense in this context, because it doesn't know (but hopefully) it <% %>

should be logically equal in javascript. There are a few things you can do to "hack" around this, such as setting up a facade javascript object that reflects the view and returning the object to the view values, although in most cases this is much more than worth it.



0


source


It will be open at the end of the day.

But maybe you can use a controller JavaScriptResult

0


source







All Articles