Amasuriel
Amasuriel

Reputation: 2200

HTML Helper with HTML Helpers Inside

I'm trying to wrap my head around something; maybe someone here can point me in the right direction.

Currently a have a form that has some controls on it like this:

@Html.ListBoxFor(s => s.SomeListSelection1, new MultiSelectList(Model.SomeList1, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection2, new MultiSelectList(Model.SomeList2, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection3, new MultiSelectList(Model.SomeList3, "Id", "Text"))
<br />

This form appears in many places with some slight variation on which controls are available.

The "slight variation" makes me not just want to do this as a partial view as I will either need to replicate the controls into many slightly varied partials or code the variation logic into the view.

So I think, "I know, I'll use the cool HTML Helper thingy!!"

But, all the HTML helper samples online are very simple html content. So the question is how can I use other HTML helpers inside an HTML helper? Or, if that is the wrong question is there another cleaner alternative to just making this a partial view with a bunch of conditional rendering logic?

Edit: While the answer did technically answer my question, I had trouble making use of the blog entries for what I was trying to do. I found a better example (for me anyway) here: ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

Upvotes: 1

Views: 796

Answers (1)

SLaks
SLaks

Reputation: 887215

Yes, you can.
Helpers can contain arbitrary Razor markup.

In fact, the direct contents of a helper method is a code block, not markup.
For a more in-depth discussion about how helpers work, see my blog.

Upvotes: 1

Related Questions