views:

540

answers:

2

Today I spent a good three hours trying to convert the project MvcContrib.Samples.InputBuilders, included in MVC Contrib to make it work with Spark View Engine, but so far was unable to do so.

Does anybody have a clue why these two just won't get along?

Changes I've made

InputForm.spark:

<viewdata model="SampleInput" />
!{Html.InputForm()}

Global.asax.cs:

...
protected void Application_Start() {
    RegisterRoutes(RouteTable.Routes);
    InputBuilder.BootStrap();
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new SparkViewFactory());
}

Web.config:

...
<spark>
    <compilation debug="true"/>
    <pages automaticEncoding="true">
        <namespaces>
            <add namespace="System"/>
            <add namespace="System.Collections.Generic"/>
            <add namespace="System.Linq"/>
            <add namespace="System.Web.Mvc"/>
            <add namespace="System.Web.Mvc.Ajax"/>
            <add namespace="System.Web.Mvc.Html"/>
            <add namespace="System.Web.Routing"/>
            <add namespace="MvcContrib.UI.InputBuilder"/>
            <add namespace="MvcContrib.UI.InputBuilder.Views"/>
            <add namespace="Web.Models"/>
        </namespaces>
    </pages>
</spark>

(I copied the last three namespaces from the sample project.)

Errors I'm getting

Depending on the order in which I setup Spark/InputBuilder in Global.asax.cs, I get two different exceptions.

If I first setup InputBuilder, then Spark (code shown above):

error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'InputForm' and no extension method 'InputForm' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

If I first setup Spark, then InputBuilder:

The view 'InputForm' or its master could not be found. The following locations were searched:

~/Views/Home/InputForm.aspx

~/Views/Shared/InputForm.aspx

~/Views/InputBuilders/InputForm.aspx

~/Views/Home/InputForm.ascx

~/Views/Shared/InputForm.ascx

+1  A: 

Your on the right track with setting up the input builders and then the spark view engine. You can see from the source file from mvccontrib that you need this namespace defined MvcContrib.UI.InputBuilder.Views for your view to reference the input builders HtmlHelper Extentions.

http://github.com/mvccontrib/MvcContrib/blob/master/src/MVCContrib/UI/InputBuilder/Views/HtmlExtensions.cs

I wrote the input builders but I do not know enough about the spark view engine to know why it is not resolving the reference to the extention methods for the input builders.

Eric Hexter
Hey Eric, thanks so much for your time. Unfortunately I already am importing MvcContrib.UI.InputBuilder.Views as you can see in my Web.config; the last three imports are copied right from the source file mvccontrib sample project. Any other clues?
Daniel Liuzzi
+2  A: 

Change input.spark to:

<use namespace="MvcContrib.UI.InputBuilder"/>
<use namespace="MvcContrib.UI.InputBuilder.Views"/>
<add namespace="Web.Models"/>
<viewdata model="SampleInput" />
!{Html.InputForm()}

Adding to web.config doesn't work in Spark. You can use _global.spark instead.

There is also another problem. In stable Spark, SparkView Html property is of type HtmlHelper, not HtmlHelper<TModel>. Html.InputForm() function works only for HtmlHelper<TModel>, so you will have to download Spark source and use development build, because it was changed recently. You can also download stable sources and change it yourself. Here is some info:

http://groups.google.com/group/spark-dev/browse%5Fthread/thread/618bd44a94368d22/f7df24e52924f4dc?show%5Fdocid=f7df24e52924f4dc

LukLed
This is exactly the kind of details I've been hunting for! The master revision from GitHub worked flawlessly. FYI Spark *does* support adding to doing the imports in web.config so you don't have to repeat them in all your views. All you need to do is reference SparkSectionHandler in configSections. Anyway, thanks a lot for such an informative and useful answer! Cheers.
Daniel Liuzzi
You can also use the stable build and create your own, derived SparkView class that exposes `HtmlHelper<TModel>`: http://groups.google.com/group/spark-dev/browse_thread/thread/d59e8675d9a78916/747c170bd7932064?pli=1
Thomas G. Mayfield