20

I'm using Chromatron theme for an admin panel in my application. There is a sidebar gadget that has HTML content and with a little CSS trick it can be shown completely different.

<section class="sidebar nested">
    <h2>Nested Section</h2>
    <p>Lorem ipsum dolor sit amet, conse ctetur adipiscing elit. Maec enas id augue ac metu aliquam.</p>
    <p>Sed pharetra placerat est suscipit sagittis. Phasellus <a href="#">aliquam</a> males uada blandit. Donec adipiscing sem erat.</p>
</section>

I want to have a Partial View that is used like this:

@Html.Partial("Path/To/Partial/View"){
    <h2>Nested Section</h2>
    <p>Lorem ipsum dolor sit amet, conse ctetur adipiscing elit. Maec enas id augue ac metu aliquam.</p>
    <p>Sed pharetra placerat est suscipit sagittis. Phasellus <a href="#">aliquam</a> males uada blandit. Donec adipiscing sem erat.</p>
}

TBH, I want to have functionality like I have in a @for(...){ } block. Is this possible in Razor?

3
  • 1
    I don't know if it's what you want, but take a look: lostechies.com/hugobonacci/2011/07/11/templates-with-razor Mar 6, 2012 at 17:58
  • Thank you Felipe. but this is not what I'm looking for, or perhaps I can't get it to work. I need the same functionality as for blocks. I want to have a trailing block where I can insert other @ razor directives or HTML content; exactly like a for block...
    – Achilles
    Mar 7, 2012 at 11:07
  • I'm trying to do the same thing; see my very similar SO question. The closest thing I've found is the post @FelipeOriani mentioned. An HtmlHelper extension method works too, but I haven't figured out to store the HTML it uses as a view or partial view. But, really, I just had to give up the bracket block syntax for the helper function syntax. Feb 21, 2014 at 19:20

2 Answers 2

31

I was having the same dilemma. Try creating a viewmodel type with a Func property and then pass the html as delegate.

public class ContainerViewModel
{
    public String Caption { get; set; }
    public String Name { get; set; }
    public Int32 Width { get; set; }
    public Func<object, IHtmlString> Content { get; set; }
}

@Html.Partial("Container", new ContainerViewModel()
{
    Name = "test",
    Caption = "Test container",
    Content =  
    @<text>
       <h1>Hello World</h1>
    </text>,
    Width = 600
})

You can call it like this in your partial.

@Model.Content(null)

If you want to be fancy you can add this extension method.

public static class PartialExtensions
{
    public static IHtmlString Display<T>
        (this T model, Expression<Func<T, Func<Object, IHtmlString>>> content) 
    {
        var compiled = content.Compile();
        return compiled.Invoke(model).Invoke(null);
    }
}

Then, any time you use this pattern, you can call it like this in your partial (not fully tested).

@model ContainerViewModel
@Model.Display(m => m.Content)  // Use delegate property

Hope this works for you.

It works because the @... syntax creates a little HtmlHelper for you that consumes a Model (which you're declaring here as type object, and passing null for), and returns an IHtmlString.

BEWARE form values don't seem to post to the server if @Html.BeginForm is used in the content.

Therefore, wrap your form around the container.

3
  • 3
    I love this solution but the <dynamic, ... felt really dirty, so I did some poking. It turns out you're creating an implicit little HtmlHelper that takes a Model, so the type of the input arg to the Func is anything you like (object if you want), and the return type is always IHtmlString. Edited answer to reflect this. Works perfectly! Aug 2, 2014 at 3:45
  • This solution is absolutely brilliant! I can create my widgets/partials with surrounding HTML content and inject the content inside it using this method. It's clean and works really well! Thank you!
    – Kevin Dark
    Aug 29, 2016 at 11:20
  • excellent solution where nothing works for child action this works perfectly as parameter, Thank You
    – Ali Jamal
    Jun 29, 2019 at 16:39
0

It seems like you want some sort of htmlhelper function to generate that content. Have you looked into implementing it as a helper instead of a partial view?

1
  • I've looked into "Inline Helpers" and it does the job; there actually are a few ways to pass content to a place holder; but I think my HTML code will look much cleaner if I could use a notation like what i've shown above.
    – Achilles
    Mar 30, 2012 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.