Asp.net无法创建视图IEnumerable <string>

时间:2015-09-07 08:09:36

标签: c# asp.net asp.net-mvc

我跟随一本关于C#的书(Pro asp.net MVC 3框架)它有点过时但与新版本相比并没有多大区别。

电子书:Ebook version, google link. 我有这本书的纸质版本,但页面编号是相同的。 我被困在第205页,它要求我创建一个名为&#39;菜单&#39;并且使用模型类IEnumerable<string>我无法创建,当我输入时,我无法点击“添加&#39;

根据这本书:

Image from book

根据我的编辑:

Image from my editor.

我是C#的新手,但我会粘贴一些代码让你们更好地理解它。

{
    public class NavController : Controller
    {
      private IProductRepository repository;
      public NavController(IProductRepository repo)
      {
        repository = repo;
      }

      public PartialViewResult Menu() //Krijg hier geen VIEW met: "IEnumerable<string>" als Modelclass.
      {
        IEnumerable<string> categories = repository.Products
                                 .Select(x => x.Category)
                                 .Distinct()
                                 .OrderBy(x => x);
        return PartialView(categories);
      }
    }
}

希望这只是愚蠢的我做错了。

欢迎提示和调整!

祝你好运

1 个答案:

答案 0 :(得分:1)

您只需创建一个视图,然后指定Model

@model IEnumerable<string>
@foreach (var str in Model)
{
   <li>
      @Html.LabelFor(m => str, "My Label")
      @Html.TextBoxFor(m => str)
      @Html.ValidationMessageFor(m => str)
   </li>
}

正如Stephen提到的那样,不需要那样指定模态。

相关问题