c# - 是否可以从 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)
);
}
解决方案
假设这段代码在一个控制器中,你可以通过调用controllerbase的Url属性来设置锚的href(实现一个IUrlHelper)
例如:new XAttribute("href", Url("ActionName", "ControllerName", new { date = d }))
推荐阅读
- c# - Xamarin Forms 在编辑器控件中获取光标位置
- node.js - 未捕获的语法错误:无法在模块外部使用导入语句 CHROME EXTENSION MV3
- asp.net-mvc - 通过 pubxml 文件在 Web 部署期间使用 app_offline 关闭网站
- postgresql - 使用最早日期提取最早记录并过滤其他值
- pytorch - Torch.dist 集体通信结果错误
- python - 如何以与在多个 GPU 上运行的多个衍生进程以及每个 GPU 处理的批量数据的输入相同的顺序获得输出?
- python - 如何将 URL 值从模板传递到 Django 中基于类的视图中的 form_valid 方法
- php - 动态调用html复选框对应的函数
- node.js - 是否可以将gridjs结果定向到控制台
- r - R将向量拆分为重叠的子向量并连接第一个和最后一个元素