Reputation: 2590
I have a partial view working with a strongly typed model. Is it possible to map my model to my partial view on the fly in a html helper method and return the rendered html?
Here is the pseudo code that I'm wondering if it's possible.
public static MvcHtmlString ContentRating(this HtmlHelper html, ContentKey contentKey)
{
ContentRatingModel contentRatingModel = new ContentRatingHelper().GetContentRatingModel(contentKey);
// map my partial view which is named "ContentRating.cshtml" to contentRatingModel
return new MvcHtmlString(string.Format("the html output of mapping");
}
And use this helper method in my views as below:
@Html.ContentRating(ContentKey.Test)
Upvotes: 0
Views: 120
Reputation: 1038820
It's not quite clear what exactly you mean by mapping a partial view to a model but if you want to render the contents of this partial view inside your helper you could do the following:
public static MvcHtmlString ContentRating(this HtmlHelper html, ContentKey contentKey)
{
ContentRatingModel contentRatingModel = new ContentRatingHelper().GetContentRatingModel(contentKey);
var result = html.Partial("ContentRating", contentRatingModel);
return new MvcHtmlString(result.ToHtmlString());
}
Don't forget to bring the System.Web.Mvc.Html
namespace in scope so that the Partial extension method could be resolved in your custom helper:
using System.Web.Mvc.Html;
Upvotes: 2