Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no intellisense in ASP.Net MVC 2.0 when assigning Model values to JavaScript?

I'm trying to add some Model properties into my JavaScript within my content page:

$(document).ready(function () {
    createPager(1, <%=Model.TotalPages %>);
    createUnprocessedPager(1, <%=Model.TotalUnprocessedPages %>);
});

Does anyone know if this is by design? Are you not meant to combine Model properties with JavaScript? Or is this a bug?

This works as expected. However, I do not have any Intellisense within the <% ... %> tags when actually writing the code. If I write any code within <script> tags, then there's no Intellisense. If I go directly under the tag </script> and type <% Model.... %> then boom, I have Intellisense again.

UPDATE: 22/10/2010

Just read Scott Guthrie's latest blog post and it appears this functionality is coming out soon with the up coming release of ASP.Net MVC 3 (possibly for the beta as well):

Note: Visual Studio Code/Markup Intellisense and Colorization within Razor files aren’t enabled yet with the Beta earlier this month. You’ll see this show up in a few weeks though – and it will support full code intellisense for HTML, JavaScript, CSS and C#/VB code within Razor files.

like image 631
djdd87 Avatar asked Aug 21 '10 15:08

djdd87


2 Answers

There is also no syntax highlighting I think. Not sure if that's a bug or a feature, but AFAIK, combining the code this way isn't a good practice. Generally inline javascript is not a good practice, but if you go with it, combine Model properties with it, and later decide to extract the scripts into a separate js file, your code will break. Therefore, it is quite common to populate hidden fields with your Model properties and read them in your js with jQuery, e.g.:

<input type="hidden" id="valTotalPages" value="<%=Model.TotalPages %>" />
<input type="hidden" id="valTotalUnprocessedPages" value="<%=Model.TotalUnprocessedPages %>" />

... 

// in js
$(document).ready(function () {
    createPager(1, $("#valTotalPages").val());
    createUnprocessedPager(1, $("#valTotalUnprocessedPages").val());
});

So lack of syntax highlighting and intellisense might be a bug, but might as well be a way of discouraging certain code patterns.

like image 98
Yakimych Avatar answered Nov 15 '22 16:11

Yakimych


You will loose you Intellisense in the views inside quotes "" like attributes.

<input type="text" value="<%= DateTime.Today.ToShortDateString() %>" />

or if it appears inside of Javascript blocks.

<script type="text/javascript">
    <%= DateTime.Today.ToShortDateString() %>
    </script>

It is my opinion that there should be Intellisense in these scenarios, so I would say it is a bug and hope future updates to Visual Studio will address and resolve this.

like image 21
John Egbert Avatar answered Nov 15 '22 16:11

John Egbert