5

I want to pass in HTML to my partial view, like so:

@Html.Partial("_MyForm", "<button id='foo'>Process Data</button>")

This currently works, but passing in strings is inelegant. Previously, IHTMLString allowed you to do this instead (see link):

public class HTMLViewModel
{
    public Func<object, IHtmlString> Content { get; set; }
}

@Html.Partial("_MyForm", new HTMLViewModel()
{
    Content =  
    @<button>
       <h1>Hello World</h1>
    </button>
})

IHTMLString no longer exists in .NET Core, can the approach still be replicated with some other feature?

1 Answers1

4

You can use IHtmlContent to maintain your current approach:

public Func<object, IHtmlContent> Content { get; set; }

And then in the partial:

@model HTMLViewModel
@Model.Content(null)

Depending on which version of .NET Core you're using you could also take advantage of some of the inline function markup changes discussed here: https://github.com/aspnet/AspNetCore-Tooling/pull/334

Walter
  • 1,855
  • 18
  • 29