View在viewmodel中无法识别类

时间:2017-04-12 19:14:25

标签: asp.net-mvc mvvm

正如您在代码中看到的,我在RegistratieViewModel中有两个类。当我将viewmodel传递给我的视图时,它只识别Selectie类而不是Reizigers?有人可以帮我吗? 我收到错误:' CS1061 C#不包含定义,也没有接受第一个类型参数的扩展方法(你是否缺少using指令或汇编引用?)'

视图模型

   public class RegistratieViewModel
   {
       public SelectieViewModel Selectie { get; set; }

       public List<ReizigersViewModel> Reizigers { get; set; }

       public RegistratieViewModel()
       {
           Reizigers = new List<ReizigersViewModel>();
       }
   }  

   public class SelectieViewModel
   {
       public SelectList VanStad { get; set; }

       public SelectList NaarStad { get; set; }

       public bool Klasse { get; set; }

       public DateTime DatumHeenReis { get; set; }

       public DateTime DatumTerugReis { get; set; }

       public int AantalReizigers { get; set; }
   }

   public class ReizigersViewModel
   {
       public string Voornaam { get; set; }

       public string Familienaam { get; set; }
   }

控制器

    // GET: Hotels/Index
    public ActionResult Index(RegistratieViewModel registratie)
    {
        stedenServices = new StedenServices();
        RegistratieViewModel registratieviewmodel = new RegistratieViewModel();
        registratieviewmodel.Selectie.VanStad = new SelectList(stedenServices.All(), "StadID", "Stad");
        registratieviewmodel.Selectie.NaarStad = new SelectList(stedenServices.All(), "StadID", "Stad");
        return View(registratieviewmodel);
    }

查看

@model Treinreizen.Models.ViewModels.RegistratieViewModel

<div class="form-horizontal">
    <h4>RegistratieViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.Reizigers.Voornaam, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reizigers.Voornaam, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reizigers.Voornaam, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reizigers.Familienaam, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reizigers.Familienaam, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reizigers.Familienaam, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

问题是您在ReizigersViewModel上引用List<ReizigersViewModel>上的媒体资源。该列表没有该属性,只有该列表中的单个项目。您无法为整个集合创建一组字段。您必须遍历集合:

@for (var i = 0; i < Model.Reizigers.Count(); i++)
{
    <div class="form-group">
        @Html.LabelFor(model => model.Reizigers[i].Voornaam, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reizigers[i].Voornaam, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reizigers[i].Voornaam, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reizigers[i].Familienaam, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reizigers[i].Familienaam, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reizigers[i].Familienaam, "", new { @class = "text-danger" })
        </div>
    </div>
}

请注意在HtmlHelper表达式中使用索引语法。现在,您引用List<ReizigersViewModel>中的单个项目,即ReizigersViewModel,而不是整个列表。