0

SparkViewエンジンでviewdata構造を使用する方法を理解できないようです。コントローラに次のコードがあります。

        // 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>());

ユニットテストで正しいプロジェクトが返され、非スパークビューが完全に機能したため、このコードは機能します。現在、Spark View Engineに切り替えようとしていますが、構文が混乱しています。補足として、sparkビューエンジンが機能し、.sparkビューを読み取っていることを確認しました。

これが私のlist.sparkビューで使用しているものです:

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

このビューをレンダリングすると、次のエラーが発生します。

.../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!

4

1 に答える 1

1

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

于 2010-02-19T04:06:49.193 に答える