首页 > 解决方案 > 是否可以从 XDocument XElement 调用操作?

问题描述

我在 .NET Core 中使用 XDocument 生成日历,并将其作为标签助手传递给视图。

我想让每一天都可点击,并将每一天的日期作为 Id 传递给另一个控制器中的操作。这可能使用 XDocument 还是只能生成原始 HTML?

理想情况下,我想做的事情是这样@Html.ActionLink("", "ActionName", "ControllerName", new { date = d })的:每天。

我尝试将<div>with包装class="day"<a>XAttribute 中,d并使用href- 将日期变为可点击并在 URL 中传递 DateTime,但我不知道如何调用特定操作。

var startDate = monthStart.AddDays(-(int)monthStart.DayOfWeek);
var dates = Enumerable.Range(0, 42).Select(i => startDate.AddDays(i));

foreach (var d in dates)
{
    if (d.DayOfWeek == DayOfWeek.Sunday && d != startDate)
    {
        yield return new XElement("div",
            new XAttribute("class", "w-100"),
            String.Empty
        );
    }

    var mutedClasses = "d-none d-inline-block bg-light text-muted";
    yield return new XElement("div",
        new XAttribute("class", $"day col p-2 border-top-0 text-truncate {(d.Month != monthStart.Month ? mutedClasses : null)}"),
        new XElement("h6",
            new XAttribute("class", "row align-items-center"),
            new XElement("span",
                new XAttribute("class", "date col-1"),
                d.Day
            ),
            new XElement("span",
                new XAttribute("class", "col-1"),
                String.Empty
            )
        ),
        GetEventHtml(d)
    );
}

标签: c#asp.net-coreasp.net-core-mvc

解决方案


假设这段代码在一个控制器中,你可以通过调用controllerbase的Url属性来设置锚的href(实现一个IUrlHelper)

例如:new XAttribute("href", Url("ActionName", "ControllerName", new { date = d }))


推荐阅读