views:

200

answers:

2

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.

+5  A: 

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.

Yakimych
Note: you will still lose intellisense when you do `value="<%=Model.TotalPages %>"`. This happens whenever you wrap `<% %>` in quotes.
Baddie
Any way around it? Or is it a case of getting a visual studios plugin?
GenericTypeTea
introducing extract hidden fields is not a good practice. it increases page size. I would rather live with no Intellisense .vote down.
Russel Yang
@Russel Yang: It's not just an intellisense issue. How would you access the value from an external js file otherwise?
Yakimych
@Russel: Yakimych is right. You don't want to introduce globals do you? I know don't because sooner or later someone else might use the same names as well.
Robert Koritnik
@Russel Yang: This technique might actually reducing the total download size because you're moving the javascript out of the page and into a cacheable external js file.
Charlino
I agree with @Russel. @Yakimych - you don't need to introduce globals, use namespaces. If you include the js file that contains the logic, you can access its properties. We do stuff like this: `PortletBrowser.l18n.Buttons[0].caption = <%= Resources.GetStirng(..) %>`. Then in the javascript you access them. Collisions don't occur because they're encapsulated within their relevant class.
Michael Shimmins
+1  A: 

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.

JohnEgbert