2

I have a C#-MVC project. I want to refresh the page every X second - I put in the cshtml file the code:

<script type="text/JavaScript">
   timedRefresh(X);
</script>

but I need to take X from C#, let's say it's "ViewBag.Seconds". How can I do this?

4 Answers 4

4

Razor doesn't care if it's outputting HTML or javascript, so you could do:

<script type="text/JavaScript">
   timedRefresh(@(ViewBag.Seconds));
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! I tried <script type="text/JavaScript"> timedRefresh(@ViewBag.Seconds); </script> and it didn't work.. so I asked here :)
2

If you are using the Razor syntax it can be done like this:

<script type="text/JavaScript">
  timedRefresh(@(ViewBag.Seconds));
</script>

The IntelliSense may report an error or warning, but it works anyway.

Comments

1

It's pretty easy

<script type="text/JavaScript">
   timedRefresh(@ViewBag.Seconds);
</script>

Comments

0

You can use an Action too. like this:

<script type="text/JavaScript">
   timedRefresh(@(Html.Action("Action","Controller")));
</script>

Comments

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.