Hi all,
New to MVC so forgive me if the terminology is a little off.
I'm using ASP.NET MVC3 beta and VS 2010.
I'm not sure if this is an error of concept, of syntax, or what.
Essentally, what I'd like to do is, for _layout.cshtml, I would like to include the jQuery script for any Controller whose ActionResult sets ViewModel.UseJQuery to true.
I'm new to things, so I could be wrong, but it seemed like the best way to do this (what I'm currently attempting) would be:
_layout.cshtml file:
@if(View.UseJQuery)
{
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
// This is more like it! SHOULDN'T BE SEEN IF UseJQuery is false!
});
</script>
}
In the Various Controllers
public ActionResult Index()
{
ViewModel.UseJQuery = false;
ViewModel.Title = "Image Gallery";
GalleryModel gm = new GalleryModel();
ViewModel.Testy = gm.TestString;
return View();
}
However, this gives me an error about having to convert a null to a boolean (I assume it's not finding the UseJQuery flag).
So, my question is two-fold:
- Is this the correct way to go about this?
- If so, where am I going wrong syntactically?
I'm sure this is probably just beginner's pains but I looked around and couldn't find a solution at first (I have an ASP.NET MVC book on order -- promise!)
Thanks in advance for any help!
Edit/Update:
Is this something that might be different between MVC 2 and 3? For example, out of the box, the Index() ActionResult of the HomeController.cs is:
public ActionResult Index()
{
ViewModel.Message = "Welcome to ASP.NET MVC!";
ViewModel.Title = "Home";
return View();
}
EDIT / UPDATE: Problem Found!
D'oh! I realized that the code works when the variable is set, but I've been trying to experiment with the variable not set (which of course causes a null value to be passed instead of a false).
So, the question now is, what logic do I put in _layout.cshtml that will allow me to capture a null and set it to false instead?
I'm thinking something along the lines of:
@if(View.UseJQuery.IsNull()){ @View.UseJQuery = false; }
However, a few issues with this:
- is IsNull() the correct function, or is my syntax wrong? (lack of syntaxsupport for Razor in VS 2010 is killing me, haha)
- How do I set the UseJQuery variable locally in the layout? I doubt View.UseJQuery will work because that's something the controller sets, right?
At any rate, the error I'm getting trying to set the above value is "Invalid expression term '='", so I believe the ViewModel variable collection may be read-only to the View?
-- Sean