简体   繁体   English

如何将HTML传递到Razor部分视图?

[英]How to pass in HTML to a Razor Partial View?

I want to pass in HTML to my partial view, like so: 我想将HTML传递给我的局部视图,如下所示:

@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 ): 以前,IHTMLString允许您这样做(参见链接 ):

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? .NET Core中不再存在IHTMLString,是否仍可以使用其他功能复制该方法?

You can use IHtmlContent to maintain your current approach: 您可以使用IHtmlContent来维护当前的方法:

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 根据您使用的.NET Core版本,您还可以利用此处讨论的一些内联函数标记更改: https//github.com/aspnet/AspNetCore-Tooling/pull/334

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM