views:

399

answers:

2

Using ASP.NET MVC & Spark, I have a view that is listing a number of searches. The view has the following declaration at the top:

<viewdata model="IEnumerable<SearchModel>" />

On the same search page, I also render a partial which is used as the content of a popup window that enables users to add new searches.

My problem is: in the partial view I want to make use of strongly typed HTML helpers and write:

${Html.TextBoxFor(model => model.SearchPhrase)}

But when I add another:

<viewdata model="CreateSearchModel" />

at the top of the partial view file, Spark fails with Only one viewdata model can be declared.

I can use normal HTML helpers, but how might I get the benefit of using strongly typed HTML helpers in this partial view - or is there a completely better way of doing this?

+6  A: 

Instead of <use file="partial"/> try to do

# Html.RenderPartial("partial", mydata);

This should workaround the single Model limitation.

queen3
Many thanks, that worked. Out of curiosity, is this a limitation of Spark then?
Martin
Yes it is Spark limitation. All the used partials are included at compile time into the resulting page .cs, so there's single Page class and single Model type. When you use RenderPartial, partial is rendered at runtime, thus no limitation. So I'd say, it's not Spark limitation - it's Spark's additional feature with its own limitation ;-)
queen3
A: 

can't you make your partial view header be this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CreateSearchModel>" %>

and use

<% Html.RenderPartial("../PartialView", (CreateSearchModel)ViewData["MyCreateSearchModel"] ); %>
hunter
You posted Web Forms view engine code for a Spark view question. So, no, he can't make the partial view header what you specified. Spark compiles subviews into a single class, so it can only have one TModel. Using Html.RenderPartial() bypasses compiling the subview into the parent.
Thomas G. Mayfield