views:

360

answers:

1

I can't seem to get a grasp on how to use the viewdata structure in the spark view engine. I have the following code in my controller:

        // Retrieve the project list from the database
        var projects = from p in _context.Repository<project>()
                       orderby p.name ascending
                       select p;

        return View(projects.ToList<project>());

This code works, as I have unit tests returning the correct projects, and my non-spark view worked perfectly. Now I am trying to switch to Spark View Engine and I'm just confused on the syntax. As a side note, I have verified that spark view engine is working and reading my .spark view.

Here is what I am using in my list.spark view:

<h2>Available Projects</h2>
<viewdata model="IList[[project]]"/>
Count: ${model.count}

When rendering this view the following error occurs:

.../List.Spark(3,16): error CS0103: The name 'model' does not exist in the current context 

This is referring to the model.count line. Why doesn't this work? I tried passing the project list to the ViewData["projects"] (and replaced model in the spark code with projects) and I get the same error (take out the model.count for projects.count).

This is probably something stupid, but I can't seem to figure this out.

Update:

Well I fixed this. It seems that the MVC2 web.config file created by VS 2010 Beta 2 was bad. I used a MVC2 web.config file created by VS 2010 RC and it now works. Thanks!

+1  A: 

I think you want this:

<h2>Available Projects</h2>

<viewdata model="IList[[project]]"/>
Count: ${ViewData.Model.Count}

or this:

<h2>Available Projects</h2>

<viewdata model="IList[[project]]"/>
<var model="ViewData.Model" />
Count: ${model.Count}

The viewdata element declares the types of the entries in the ViewDataDictionary. For "model", this is actually declaring the type of the ViewDataDictionary's Model property.

Note also that these expressions and type names are C# code, and thus case sensitive.

EDIT: syntax updated for 1.0 stable release.

Reference - using view data in the documentation

Lachlan Roche
Doesn't work, I still get "error CS0103: The name 'Model' does not exist in the current context". Also, the project inside of the IList is meant to be lower case, as it's the entity class name with lowercase generated by linq2sql.
KallDrexx
Hmm. I am running v1.0.39935, build date 13/01/2010.
Lachlan Roche
I am running v1.0.39970 (built on 17/02/2010).
KallDrexx
The current source on github has a Model property on SparkView<T> but not on SparkView. Both have the ViewData property.
Lachlan Roche
Well, your new 2nd example (using the var) gives the following error: "c:\Users\KallDrexx\Documents\Projects\Scrawl\Scrawl\Areas\Writing\Views\Project\List.Spark(5,23): error CS0103: The name 'model' does not exist in the current context". I am beginning to think the latest source is fubared as that looks like all vars aren't working.
KallDrexx
Tomorrow (since I probably won't have time today) I will download the 39935 build and see if that fixes the issue.
KallDrexx
Also, for anyone interested (possibly as of version 1.1) you can also do `<viewdata model="IList[[project]] model"/>` and it will create a property named "model" without the explicit variable declaration
R0MANARMY