【问题标题】:Non-invocable member "HomeController.ServicesList" cannot be used like a method不可调用的成员“HomeController.ServicesList”不能像方法一样使用
【发布时间】:2016-04-11 04:19:19
【问题描述】:

我想从 Cshtml 获取服务信息。但我得到了错误。

public class HomeController : Controller
    {

        [HttpGet]
        public ActionResult Index()
        {
            ServiceController[] Services;
            Services = ServiceController.GetServices();

            ServicesViewModel servicesViewModel = new ServicesViewModel();
            ServisModel servisler = new ServisModel();
            List<ServicesViewModel> list = new List<ServicesViewModel>();

            foreach (ServiceController svc in Services)
                {                
                    servicesViewModel.ServiceName = svc.ServiceName;
                    servicesViewModel.ServiceDisplayName = svc.DisplayName;
                    servicesViewModel.ServiceStatus = svc.Status;
                    list.Add(servicesViewModel);
            }            

            return View(ServicesList(list));            
        }



        public class ServicesList : IEnumerable
        {
            List<ServicesViewModel> liste = new List<ServicesViewModel>();             

            public IEnumerator GetEnumerator()
            {
                return new MyEnumerator(liste);
            }
        }

错误:CS1955 不可调用成员“HomeController.ServicesList”不能像方法一样使用。

这是 MyEnumerator 类:

public class MyEnumerator : IEnumerator
    {
        List<ServicesViewModel> lst = new List<ServicesViewModel>();
        int CurrentLocation = -1;

        public MyEnumerator(List<ServicesViewModel> p) {
            this.lst = p;
        }


        public object Current
        {
            get
            {
                return this.lst[CurrentLocation];
            }
        }

        public bool MoveNext()
        {
            CurrentLocation++;
            return (CurrentLocation < this.lst.Count);
        }

        public void Reset()
        {
            CurrentLocation = -1;
        }
    }

最后这是cshtml文件:

@model IEnumerable<ExampleProject.ViewModel.ServicesViewModel> 
@{
    Layout = "~/Views/shared/_Layout.cshtml";
    ViewBag.Title = "Sunucu Yönetim Paneli | Ana Sayfa";
    ViewBag.Description = "Sunucu Yönetim Paneli";
    ViewBag.Keywords = "sunucu, yönetim,paneli";
}

@using (Html.BeginForm("Ara", "Home", FormMethod.Get))
{
    <p>
        Aranacak Kelime: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
        <input type="submit" value="Ara" />
    </p>
}

<table class="table">
    <tr>
        <th>
           Servis Adı
        </th>
        <th>
            Servis Açıklaması
        </th>
        <th>
            Servis Durumu
        </th>
        <th>
            Servis Başlangıç Türü
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @item.ServiceName
            @*@Html.DisplayFor(modelItem => item.allServices)*@
        </td>
        <td>
            @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
        </td>
        <td>
            @*@Model.ServiceStatus*@
            @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
        </td>
        <td>
            @*@Model.ServiceStartMode*@
            @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
        </td>
        <td>
            @*@Html.ActionLink("Başlat", "ServiceStart", "ServicesStartStop", new { @id = item.ServiceName }) |
            @Html.ActionLink("Durdur", "ServiceStop", "ServicesStartStop", new { @id = item.ServiceName }) |
            @Html.ActionLink("", "", "Başlatma Türü", new { @id = item.ServiceName }, null)*@
            @*<input type="submit" value="Başlat" />
            <input type="submit" value="Durdur" />
            <input type="submit" value="Başlatma Türü" />*@
        </td>
    </tr>
}

</table>

【问题讨论】:

  • 你想用ServicesList(list)做什么?你的意思是new ServicesList(list) (但它不起作用)? MyEnumerator 是什么?

标签: c# .net razor ienumerable ienumerator


【解决方案1】:
public class MyEnumerator : IEnumerator
        {
            List<ServicesViewModel> lst = new List<ServicesViewModel>();
            int CurrentLocation = -1;

            public MyEnumerator(List<ServicesViewModel> p) {
                this.lst = p;
            }


            public object Current
            {
                get
                {
                    return this.lst[CurrentLocation];
                }
            }

            public bool MoveNext()
            {
                CurrentLocation++;
                return (CurrentLocation < this.lst.Count);
            }

            public void Reset()
            {
                CurrentLocation = -1;
            }
        }

【讨论】:

    【解决方案2】:
     My Cshtml Class.   
    
    
     @model IEnumerable<ExampleProject.ViewModel.ServicesViewModel> 
        @{
            Layout = "~/Views/shared/_Layout.cshtml";
            ViewBag.Title = "Sunucu Yönetim Paneli | Ana Sayfa";
            ViewBag.Description = "Sunucu Yönetim Paneli";
            ViewBag.Keywords = "sunucu, yönetim,paneli";
        }
    
        @using (Html.BeginForm("Ara", "Home", FormMethod.Get))
        {
            <p>
                Aranacak Kelime: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
                <input type="submit" value="Ara" />
            </p>
        }
    
        <table class="table">
            <tr>
                <th>
                   Servis Adı
                </th>
                <th>
                    Servis Açıklaması
                </th>
                <th>
                    Servis Durumu
                </th>
                <th>
                    Servis Başlangıç Türü
                </th>
                <th></th>
            </tr>
    
        @foreach (var item in Model) {
            <tr>
                <td>
                    @item.ServiceName
                    @*@Html.DisplayFor(modelItem => item.allServices)*@
                </td>
                <td>
                    @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
                </td>
                <td>
                    @*@Model.ServiceStatus*@
                    @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
                </td>
                <td>
                    @*@Model.ServiceStartMode*@
                    @*@Html.DisplayFor(modelItem => item.ServiceDisplayName)*@
                </td>
                <td>
                    @*@Html.ActionLink("Başlat", "ServiceStart", "ServicesStartStop", new { @id = item.ServiceName }) |
                    @Html.ActionLink("Durdur", "ServiceStop", "ServicesStartStop", new { @id = item.ServiceName }) |
                    @Html.ActionLink("", "", "Başlatma Türü", new { @id = item.ServiceName }, null)*@
                    @*<input type="submit" value="Başlat" />
                    <input type="submit" value="Durdur" />
                    <input type="submit" value="Başlatma Türü" />*@
                </td>
            </tr>
        }
    
        </table>
    

    【讨论】:

      【解决方案3】:

      所以,第一个错误:您尝试像方法一样使用ServicesList,但它是一个类,因此您需要在类名之前使用 new 关键字。 第二个错误:在您的ServicesList 类中,您没有以列表为参数的构造函数。

      要解决问题,您需要以这种方式更改代码:

      public class HomeController : Controller
      {
          [HttpGet]
          public ActionResult Index()
          {
              ServiceController[] Services;
              Services = ServiceController.GetServices();
      
              ServisModel servisler = new ServisModel();
              List<ServicesViewModel> list = new List<ServicesViewModel>();
      
              foreach (ServiceController svc in Services)
              {                
                  ServicesViewModel servicesViewModel = new ServicesViewModel();
                  servicesViewModel.ServiceName = svc.ServiceName;
                  servicesViewModel.ServiceDisplayName = svc.DisplayName;
                  servicesViewModel.ServiceStatus = svc.Status;
                  list.Add(servicesViewModel);
              }            
      
              return View(new ServicesList(list));            
          }
      
          public class ServicesList : IEnumerable
          {
              private List<ServicesViewModel> liste = new List<ServicesViewModel>();
      
              public ServicesList(List<ServicesViewModel> l) {
                  liste = l;
              }
      
              public IEnumerator GetEnumerator()
              {
                  return new MyEnumerator(liste);
              }
          }
      

      然后在您的视图 (cshtml) 文件中,您需要更改模型类型:

      @model IEnumerable<ExampleProject.ViewModel.ServicesViewModel>
      

      收件人:

      @model IEnumerable<ExampleProject.[PartThatIDontKnow].ServicesList>
      

      并解决与此更改相关的问题,例如:

      @foreach (var item in Model)
      

      变成:

      @foreach (var item in Model.GetEnumerator())
      

      我真的不明白你想做什么,但这样代码应该可以毫无问题地构建和运行。在您的 cshtml 文件中,我看不到任何尝试使用 ServicesList 的代码部分。我希望能有所帮助。如果一切正常,请告诉我。

      注意:当您需要向问题添加信息时,请使用编辑(在问题文本下方)而不是写答案。我为你更新了你的问题。

      【讨论】:

      • 首先,非常感谢。我的英文不是很好。我很抱歉。我更改了代码。但是有错误:错误:System.InvalidOperationException:传递到字典中的模型项的类型为“ExampleProject.Controllers.HomeController+ServicesList”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[的模型项ExampleProject.ViewModel.ServicesViewModel]'.
      • 不客气!是的,对不起!我的错。当您尝试我的解决方案时,我编辑了我的答案。您需要在视图中更改模型类型。在HomeController 代码之后查看我的答案。
      • @model IEnumerable PartThatIDontKnow?我是 IEnumerable Serviceslist 中的 ServisModel,但我的错误:错误 CS1579 foreach 语句无法对“System.Collections.Generic.IEnumerator”类型的变量进行操作,因为“System.Collections.Generic.IEnumerator' 不包含 'GetEnumerator' 的公共定义
      • 这个错误在cshtml的foreach中?如果是,为什么您在视图中使用 ServisModel 而不是 ServicesList?我刚才看到的另一件事; HomeController 中的 foreach 是错误的,因为在第二步中您更改了 servicesViewModel 的值,这也会更改列表中已经存在的值,因为具有相同的引用。我会更新答案
      猜你喜欢
      • 2013-08-10
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2016-05-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多