1

Possible Duplicate:
How to assign JavaScript value under MVC3

I try to do the following

<script type="text/javascript">

var techIDs = "";

@if (ViewBag.URLParameters != null)
{
   techIDs = '@ViewBag.URLParameters';
}

</script>

But it seems it's a wrong approach.

Any clue how it could be done?

Thank you!

2
  • 1
    @SimonWhitehead I've asked how to use ViewBag for it. And it is an another question. Commented Aug 15, 2012 at 23:28
  • 1
    Apologies. However, there is a solution in there for this very issue also. Commented Aug 15, 2012 at 23:39

3 Answers 3

9

try this

var techIDs = "";
@if (ViewBag.URLParameters != null)
{
   @:techIDs = '@ViewBag.URLParameters';
}
Sign up to request clarification or add additional context in comments.

Comments

2

I found other approach

 var techIDs = "";

   @if (ViewBag.URLParameters != null)
   {
         <text>techIDs = '@ViewBag.URLParameters' ;</text>
   }

Comments

2

Maybe:

<script>
    var techIDs = '@Html.Raw(ViewBag.URLParameters ?? String.Empty)';
</script>

1 Comment

Hi Jonathan, could you please tell me what if my string in URL Parameters contains single quote ' in it? I've applied your approach, however, it is failing when single quote gets encountered. TIA

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.