I created a partial view that uses a method called GetUrl but I got the error cannot access static method in a non static context
.
Here's how I implemented the method:
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 "#";
}
}
}
and my partial view :
@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>
}
This partial view generates a vertical menu with URLs. How can I call my static method?
You call a static method on the class itself, not an instance of the class.
<a href="@TimeLineStep.GetUrl(menuItem.Code)"> @menuItem.Title </a>
But are you sure you meant to make that static? It seems like what you wanted was:
public string GetUrl()
{
switch (this.Code)
....
which would then be called as
<a href="@menuItem.GetUrl()"> @menuItem.Title </a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.