Parminder
Parminder

Reputation: 3158

passing html to a function asp.net mvc

I have Html helper method, creating a check box along with some text, I pass.

@Html.CheckBoxFor(x => x.SomeProperty,@<text> <ul> <li> </li> </ul>           </text>}))


public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool>> expression, ?? )
    {
        var chkBox = helper.CheckBoxFor(expression).ToString();

        return MvcHtmlString.Create(string.Format("<li>{0}</li><div>{1}</div>", chkBox, ??);
    }

What would be the signature of my method then. Some lambda/expression or something.

Help will be appreciated.

Regards

Parminder

Upvotes: 2

Views: 1520

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

I would recommend you looking at templated razor delegates. So in your case the helper might look something along the lines of:

public static MvcHtmlString CheckBoxFor<TModel>(
    this HtmlHelper<TModel> helper, 
    Expression<Func<TModel, bool>> expression,
    Func<object, HelperResult> template)
{
    var chkBox = helper.CheckBoxFor(expression).ToHtmlString();
    return MvcHtmlString.Create(
        string.Format("<li>{0}</li><div>{1}</div>", chkBox, template(null))
    );
}

and in the view:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.CheckBoxFor(
        x => x.SomeProperty,
        @<text><ul><li></li></ul></text>
    )
    <input type="submit" value="OK" />
}

Upvotes: 6

Jon
Jon

Reputation: 437854

Just string. It's going into string.Format, so that gives you a hint.

Upvotes: 0

Related Questions