views:

48

answers:

3

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"&gt;
        </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:

  1. Is this the correct way to go about this?
  2. 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

+1  A: 

You don't seem to be passing your model to the view. Is _layout.cshtml set up as a strongly-typed view expecting a ViewModel object?

Carson63000
This is a directly out of the box setup, MV3 application in a brand new solution. the _layout.cshtml is the one that VS generated for me.
goober
Aha I see this is indeed an MVC3 difference - see "New Dynamic ViewModel Property" in http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx - having said that, I tried the code from your question and it all worked as expected? No errors, and it served or did not serve the script depending on the UseJQuery flag. Can you copy your exact error in?
Carson63000
Thanks for following up! I'm powering up my laptop to give the full answer, but I believe it says that it can't convert UseJQuery from 'null' to 'bool', which makes me think that value isn't being passed (I checked spelling, etc.). I'm wondering if it might be an issue that I'm trying to do it for a _layout.cshtml rather than an actual view? I'd like the logic to inherit. Maybe I need to set it false in _layout.cshtml and let the Controller override it to true when necessary? Thanks for the continued help. :)
goober
Please see my updated post for a further description of what the issue is. Thanks!
goober
Aha! Starting to make more sense now. Unfortunately, I have never used a `dynamic`, and to be honest, I never want to, so I'm not sure how you go about checking to see whether it contains a property of a certain name or not. You could use a try/catch block to catch the `RuntimeBinderException`, or you could always just set it to `false` if it's not needed, rather than leaving it undefined. If there's a more elegant solution, a quick google didn't help me find it.
Carson63000
+1  A: 

Normally I'd expect to see something similar to this:

public ActionResult Index()
{
    GalleryModel gm = new GalleryModel();

    var model = new ViewModel {
        UseJQuery = false,
        Title = "Image Gallery",
        Testy = gm.TestString            
    };

    return View(model);
}

Looking at your code, is ViewModel a class or an object with a capital V? Either way, the important bit is passing it to the View() method.

In your view page you'll want to specify that its a strongly types view, expecting a model of type ViewModel.

Normally you'd inherit from System.Web.Mvc.ViewPage<T>

Michael Shimmins
please see the edit / update -- is this an MVC v2 vs v3 issue?
goober
Please see my updated post for a further description of what the issue is. Thanks!
goober
A: 

in your controller you define:

ViewModel.UseJQuery

yet in your view you use:

if(View.UseJQuery)
Dusty Roberts
Correct. However, what I'm trying to account for is that this is in the layout page. So, certain controllers may not think to define this value, causing it to be null, causing an error to be thrown because it can't convert a "null" to a "boolean".
goober
So, what I'd like to say is: If View.UseJQuery = null, set View.JQuery to false. And later in the code, we use the if(View.UseJQuery) code. So, the question is -- the value View.UseJQuery has been passed to _layout.cshtml; how can I modify that value afterwards in order to set it to false if it's null?
goober