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?

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.