asp.net mvc 2 - return JavaScript with View
Posted
by Tomaszewski
on Stack Overflow
See other posts from Stack Overflow
or by Tomaszewski
Published on 2010-05-13T15:03:10Z
Indexed on
2010/05/13
15:54 UTC
Read the original article
Hit count: 231
Hi, using ASP.NET MVC 2 I have a navigation menu inside my Master Page. In the navigation menu, I am trying add a class to the that the current page relates to (i.e., home page will add class="active" to the Home button). I'm trying to consider scalability and the fact that I don't want to change individual pages if the navigation changes later.
The only way I can think of doing this is:
- Add JavaScript to each individual View that will add the class when the DOM is ready
- Return JavaScript when return View() occurs
on point (2), I am unsure how to do. Thusfar I have been doing the following in my controller:
public ActionResult Index()
{
ViewData["messege"] = JavaScript("<script type='text/javascript' language='javascript'> $(document).ready(function () { console.log('hi hi hi'); }); </script>");
return View();
}
but in my view, when I call:
<%: ViewData["messege"] %>
I get: System.Web.Mvc.JavaScriptResult as the result
Would you guys have any ideas on
- How to solve the navigation menu probelem, other than the solutions I've listed
- return JavaScript along with your view from the Controller
Thanks, in advanced!
© Stack Overflow or respective owner