Martin
Martin

Reputation: 43

Using a different viewmodel in partial view using Spark view engine

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?

Upvotes: 4

Views: 1446

Answers (2)

hunter
hunter

Reputation: 63532

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"] ); %>

Upvotes: 0

queen3
queen3

Reputation: 15521

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

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

This should workaround the single Model limitation.

Upvotes: 8

Related Questions