1

I have a view that contains a javascript function that i need to init/call when the page loads. The method is static but it needs to be called with some parameteres (a list of Id's) - this list is dynamic and varies by some querystring parameters (i can not generate the list clientside from the querystring).

So i need to generate this list server-side.

My options are, as far as i see it:

1) Make an ajax call on the client, requesting the ids from the server.

2) Inject/inserting the ids directly on the view (it is a property on the viewmodel).

No matter how i turn it, option 2 seems the most sane. I already got the data ready on the viewmodel and thus it's ready when the view is populated - i see no reason to make an extra request to the server, just to get the data.

I know many would think it's a bad idea to inject something dynamic into an otherwise static javascript. For that i could simply just inject a new javascript, holding only the Ids and a call to the static javascript method, which is what i really want to do.

My problem is this though: When i write my asp.net <%= %> includes, VS IDE stops highligting, making me think i might be on the wrong track? Surely i'm not the only one needing to output something in a javascript block in asp.net mvc?

  • If VS stops highlighting it doesn't mean that view parser won't pick-up server side script sentences. – Robert Koritnik Oct 1 '09 at 6:05
1

Route 2 (The ViewModel) is definately the way to go and

<script type="text/javascript>
 <%= Model.JavascriptToInsert %>
</script>

should work (despite lack of VS highlighting)

It will NOT work in a seperate JS file though. It must be in your view itself.

Kindness,

Dan

| improve this answer | |
  • I was only thinking of putting the static javascript in a seperate js file - to seperate the static javascript from the dynamic. But thanks, this is what i'm currently doing, just wondering if i'm missing something - i hate that hightlighting is not showing up... – Per Hornshøj-Schierbeck Oct 1 '09 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.