不可调用的成员“HomeController.ServicesList”不能像方法一样使用

时间:2016-01-06 22:18:09

标签: c# .net razor ienumerable ienumerator

我想从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>

3 个答案:

答案 0 :(得分:0)

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;
            }
        }

答案 1 :(得分:0)

 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>

答案 2 :(得分:0)

所以,第一个错误:您尝试像方法一样使用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的代码部分。我希望能成为一个帮助。如果一切正常,请告诉我。

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

相关问题