Just to expそしてon the accepted answer, as it took me quite a while to resolve a similar problem そしてthis is the question which popped up. What I really need was a @helper
, which would accept razor text, as the template should contain quite some code. I played around for a long while trying to use several versions of type @helper item(Func
, which I found on the web, with no success. Therefore I went for an approach like:
namespace project.MvcHtmlHelpers
{
public static class HelperExtensions
{
public static MvcHtmlString RazorToMvcString(this HtmlHelper htmlHelper, Func
そして
@project.MvcHtmlHelpers
@helper item(other input, MvcHtmlString content)
{
<div class="item">
...other stuff...
<div class="content">
@content
</div>
</div>
}
そしてuse this via
@item(other input, @Html.RazorToMvcString(@this is a test))
今度は、両方のRazor入力にヘルパーテンプレートを使用できますが、一部の点では便利な部分的なビューをドロップすることもできます。私は専門家がいないので、より良いオプションがあるかもしれませんが、それは私にとっては柔軟なアプローチのようです。