我创建了一个使用名为 GetUrl 的方法的分部 View ,但收到错误无法在非静态上下文中访问静态方法
。
这是我实现该方法的方法:
public class TimeLineStep
{
public string Code { get; set; }
public string Title { get; set; }
public TimeLineStatus Status { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public static string GetUrl(string code)
{
switch (code)
{
case "1":
return "#";
case "2":
return "#";
case "3":
return "#";
case "4":
return "#";
case "5":
return "#";
case "6":
return "#";
case "7":
return "#";
default:
return "#";
}
}
}
以及我的部分观点:
@using UI.Controls
@model List<Web.Models.TimeLineStep>
@{
Layout = null;
}
@using (Html.ContentBlock("Yellow", ""))
{
<ul>
@foreach (var menuItem in Model)
{
<li>
<a href="@menuItem.GetUrl(menuItem.Code)"> @menuItem.Title </a>
</li>
}
</ul>
}
此部分 View 生成带有 URL 的垂直菜单。如何调用我的静态方法?
最佳答案
您调用类本身的静态方法,而不是类的实例。
<a href="@TimeLineStep.GetUrl(menuItem.Code)"> @menuItem.Title </a>
但是你确定要使其静态吗?看来您想要的是:
public string GetUrl()
{
switch (this.Code)
....
这将被称为
<a href="@menuItem.GetUrl()"> @menuItem.Title </a>
关于c# - 无法在非静态上下文中访问静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31725065/