如何创建一个辅助程序,用于呈现带有模型的部分视图?

3
我会给出一个非常简单的例子:
目前我必须这样写:
<% Html.RenderPartial("hello", new HelloInput { Name = "Jimmy" } ); %>

我希望能够像这样点赞:

<%=Html.Hello("Jimmy") %>

所以我想知道如何创建这个帮助程序:
public static string Hello(this HtmlHelper helper, string name)
{
    return the result of rendering partial view "hello" with HelloInput{ Name = name };
}
1个回答

2

Partial是RenderPartial的<%=版本。

public static string Hello(this HtmlHelper helper, string name)
{
    return helper.Partial("hello", new HelloInput { Name = name } );
}

否则所有人都叫Jimmy ;),您可以将new HelloInput { Name = "Jimmy" }替换为new HelloInput { Name = name } - davehauser

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接