I searched but could not find any quick solutions for an MVC 3 htmlhelper to create a wrapper method. What I'm looking for is something like:
@html.createLink("caption", "url")
{
<html> content in tags </html>
}
the result should have
<a href="url" title="Caption">
<html> content in tags </html>
</a>
Any help with this.
There's also another way, without disposable trick. It's less work, great for small helpers. I answered similar question, and don't want to copy everything, but here's a short example:
Usage of this helper looks like this:
My full answer to the other similar question here.
At its simplest level something like this would do it
The way that this is done with BeginForm is that the return type
MvcForm
implimentsIDisposable
so that when used within ausing
statement, theDispose
method ofMvcForm
writes out the closing</form>
tag.You can write an extension method that does exactly the same thing.
Here's one I just wrote to demonstrate.
First off, the extension method:
And here's our new type, MvcAnchor:
In your views you can now do:
Which yields the result:
Expanding this slightly to deal with your exact requirement:
and our view:
which yields the result: