BradLaney
BradLaney

Reputation: 2413

Razor Helpers sharing html problem with code blocks

I guess what I want to do is "chain" my data down so that it ends up looking the same. All my html must be wrapped in some form of

<fieldset class="" data-role="">

So what I have is a helper that prints the various forms. One would be a label:

<fieldset data-role="@role">
    <label>@Html.Raw(label)</label>
</fieldset>

Now when I have multiple types of labels, and one includes being a code block. When it is a simple piece of text, like "First Name" I do:

@FieldSet.Label("First Name")

But when I have a code block such as:

<b>some text</b>
<p>some other text (some time frame - some time frame)

It becomes complicated to use this:

@FieldSet.Label("<b>" + Model.Text1 + "</b><p>" + Model.Text2 + 
    " (" + Model.Time1 + " - " + Model.Time2 +")</p>")

What I want it a solution that looks something like this:

@FieldSet.Label(@<text>
<b>@Model1.Text1</b>
<p>@Model.Text2 (@Model.Time1 - @Model.Time2)</p>
</text>)

I read somewhere this was possible, but I cannot find the article. I could be completely misled, but I really don't want to have a single piece of HTML in the code behind and I want to utilize the razor syntax, not string concatenation.

Upvotes: 5

Views: 1672

Answers (2)

GvS
GvS

Reputation: 52518

You could look at how the @Html.BeginForm is implemented.

Create a class that implements IDisposable, and that writes to the Response stream directly:

Your code could look like this (entered by head, not tested):

class FieldSet : IDisposable {
     public FieldSet(string label) {
          // TODO: Encode label on line below
          HttpContext.Current.Response.Write(string.Format("<fieldset><label =\"{0}\"", label));
     }

    public void Dispose() {
        HttpContext.Current.Response.Write("</fieldset>");
    }
}

static class FieldSetExtionsions {
    public static FieldSet FieldSet(this HtmlHelper html, string label) {
        return new FieldSet(label);
    }
}

The usage will be:

@using (Html.FieldSet("your label")) {
     <div>
         Your razor code goes here
     </div>
}

Upvotes: 3

Vasea
Vasea

Reputation: 5333

Check this articles from Phil Haack

You could:

Write as an extension method to a strongly-typed HtmlHelper:

public static class RazorExtensions
{
    public static HelperResult Label<T>(this HtmlHelper<T> helper, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(helper.ViewData.Model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

So you could write

@Html.Label(@<text><span>@Model.Item1<span><strong>@Model.Item2</strong></text>)

Pass Model as a parameter to your helper method

public static class FieldSet
{
    public static HelperResult Label<T>(this T model, Func<T, HelperResult> template) {
        return new HelperResult(writer => {
            writer.Write("<label>");
            template(model).WriteTo(writer);
            writer.Write("</label>");
        });
    }
}

Usage:

@FieldSet.Label(Model, @<div><span>@Model.UserName</span><strong>@Model.FullName</strong><p>@Model.Description</p></div>)

Upvotes: 4

Related Questions