Html Helpers 可以在 Razor 页面之外使用吗

发布于 2024-11-27 10:01:50 字数 778 浏览 2 评论 0原文

我有一个类“IncomeStatement”,用作 ViewModel。这个类有一个属性,可以生成一些相当复杂的 html,如下所示:

public MvcHtmlString HtmlPeriods { get; set; }
.... 
StringBuilder htmlPeriods = new StringBuilder(100);
htmlPeriods.AppendFormat(
"<td><a href='/Forecast/IndexPeriod?Period={1}'>{0}</a></td>",
    inc.NetSales, per.Period.PeriodID);
....
HtmlPeriods = MvcHtmlString.Create(htmlPeriods.ToString())

然后在 Razor 文件中,我使用 HtmlPeriods 属性,效果很好:

<th></th>@Model.HtmlPeriods<td></td>

但是,如果我想在我的类中使用 Html.ActionLink(...) 来创建,该怎么办?
很好的 Razorlike 链接, 像这样的事情:

string forecastLink = 
Html.ActionLink("Edit Forecast", "/Forecast/IndexEdit?PeriodID=2005Q1");

我该怎么做?

I have a class 'IncomeStatement' that is used as a ViewModel. This class has a property that generates some fairly complicated html like this:

public MvcHtmlString HtmlPeriods { get; set; }
.... 
StringBuilder htmlPeriods = new StringBuilder(100);
htmlPeriods.AppendFormat(
"<td><a href='/Forecast/IndexPeriod?Period={1}'>{0}</a></td>",
    inc.NetSales, per.Period.PeriodID);
....
HtmlPeriods = MvcHtmlString.Create(htmlPeriods.ToString())

Then in the Razor file I use the HtmlPeriods property, which works fine:

<th></th>@Model.HtmlPeriods<td></td>

But what if I want to use the Html.ActionLink(...) in my class to create
nice Razorlike links,
something like this:

string forecastLink = 
Html.ActionLink("Edit Forecast", "/Forecast/IndexEdit?PeriodID=2005Q1");

How would I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-12-04 10:01:50

您可以使用 HtmlHelper 类来执行此操作。我想您会需要 GenerateLink 方法。

例子:

string link = HtmlHelper.GenerateLink(HttpContext.Current.Request.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Default", "Index", "Home", null, null);

You can use the HtmlHelper class to do this. I think you will want the GenerateLink method.

Example:

string link = HtmlHelper.GenerateLink(HttpContext.Current.Request.RequestContext, System.Web.Routing.RouteTable.Routes, "My link", "Default", "Index", "Home", null, null);
一直在等你来 2024-12-04 10:01:50

在您的情况下,定义 HTML 帮助程序而不是在模型上使用属性和方法会更正确。例如:

using System.Web.Mvc;
using System.Web.Mvc.Html;

public static class HtmlExtensions
{
    public static MvcHtmlString HtmlPeriods(this HtmlHelper<SomeViewModel> html)
    {
        // here you can use html.ActionLink in order
        // to generate the desired markup

        // you also have access to the view model here:
        SomeViewModel model = html.ViewData.Model;

        ... 
    }
}

然后在你看来:

@Html.HtmlPeriods()

In your situation it would be more correct to define HTML helpers instead of using properties and methods on your models. For example:

using System.Web.Mvc;
using System.Web.Mvc.Html;

public static class HtmlExtensions
{
    public static MvcHtmlString HtmlPeriods(this HtmlHelper<SomeViewModel> html)
    {
        // here you can use html.ActionLink in order
        // to generate the desired markup

        // you also have access to the view model here:
        SomeViewModel model = html.ViewData.Model;

        ... 
    }
}

and then in your view:

@Html.HtmlPeriods()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文