正如您在代碼中看到的,我在RegistratieViewModel中有兩個類。當我將viewmodel傳遞給我的視圖時,它只識別Selectie類而不識別Reizigers?有人可以幫我嗎? 我得到的錯誤:「CS1061 C#不包含定義並沒有擴展方法接受一個類型的第一個參數可以找到(是否缺少using指令或程序集引用?)」視圖無法識別viewmodel中的類
視圖模型
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>
'model.Reizigers'是一個'List',而不是'ReizigersViewModel'。另外,在您的Controller方法中,您似乎沒有在任何時候向此列表中添加任何內容。 – user1935361
請發表完整的例外。具有諷刺意味的是,你省略的部分是最重要的,因爲它詳細說明了*究竟缺少什麼。一般來說,當你調用一個類型沒有的方法時,這是一個異常。它真的與你的視圖模型或任何其他類無關。目前還不清楚在你的代碼中這個異常是從哪裏提出來的。 –
@ user1935361那麼我能做些什麼來解決這個問題?目的是有人必須說明有多少旅客會來感謝int AantalReizigers。當有人給出號碼時,會自動添加「Voornaam」和「Familienaam」字段。而將要提供的名字將被列入名單。 – KevinN