c# - The model item passed into the ViewDataDictionary is of type Models but needs ViewModels
问题描述
I've been struggling with this error for a few days now for an assignment and I can't get it fixed, I've shown it to another person and we are bewildered as to what could be the issue? "InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'FYP_RSVP_MGMT.Models.GuestList', but this ViewDataDictionary instance requires a model item of type 'FYP_RSVP_MGMT.ViewModels.GuestListViewModel'."
GuestListController:
public class GuestListController : Controller
{
public IActionResult Index()
{
GuestListViewModel guest = new GuestListViewModel();
return View("Index", guest);
}
public IActionResult CreateUpdate(GuestListViewModel guest)
{
if(ModelState.IsValid)
{
using (var db = DbHelpers.GetConnection()) {
if(guest.EditableGuest.GuestID == null)
{
/* Count the existing IDs and adds the next ID */
guest.EditableGuest.GuestID = guest.Guests.Count;
db.Insert<GuestList>(guest.EditableGuest);
}
/* If the guest already exists, we are updating the details*/
else
{
GuestList dbItem = db.Get<GuestList>(guest.EditableGuest.GuestID);
TryUpdateModelAsync<GuestList>(dbItem, "EditableGuest");
db.Update<GuestList>(dbItem);
}
}
return RedirectToAction("ViewGuestList");
}
else
{
return View("Index", new GuestList());
}
}
GuestListViewModel
public class GuestListViewModel
{
public GuestListViewModel()
{
using (var db = DbHelpers.GetConnection())
{
this.EditableGuest = new GuestList();
this.Guests = db.Query<GuestList>("Select * From GuestList").ToList();
}
}
public List <GuestList> Guests { get; set; }
/* Holds any instance of an object that is being added/edited/deleted etc */
public GuestList EditableGuest { get; set; }
}
GuestList Model
public class GuestList
{
public int? GuestID { get; set; }
[Required]
public string GuestName { get; set; }
[Required]
public string GuestType { get; set; }
[Required]
public string ContactDetails { get; set; }
public bool PlusOne { get; set; }
public string PlusOneName { get; set; }
[Required]
public string Response { get; set; }
}
Index.cshtml - where form is being displayed
@model FYP_RSVP_MGMT.ViewModels.GuestListViewModel
@{
ViewData["Title"] = "Guest RSVP";
}
@using (var form = Html.BeginForm("CreateUpdate", "GuestList", FormMethod.Post))
{
<div class="container" id="GuestRSVP">
<br/>
<br/>
<br/>
<h3> Welcome to [Bride] and [Groom]s Wedding</h3>
<h4>[Church Location]</h4>
<h4>[Wedding Date and Time]</h4>
<div class="container" id="RSVPTable" style="align-content:center">
<table>
<tr>
<td>Guest Name: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.GuestName)</td>
<td>@Html.HiddenFor(m => m.EditableGuest.GuestID)</td>
</tr>
<tr>
<td>Guest Type: </td>
<td>@Html.DropDownListFor(m => m.EditableGuest.GuestType, new List<SelectListItem> { new SelectListItem { Value = "Guest", Text = "Guest" }, new SelectListItem { Value = "Wedding Party", Text = "Wedding Party" } })</td>
</tr>
<tr>
<td>Contact Details: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.ContactDetails)</td>
</tr>
<tr>
<td>Plus One: </td>
<td>@Html.CheckBoxFor(m => m.EditableGuest.PlusOne)</td>
</tr>
<tr>
<td>Plus One Name: </td>
<td>@Html.TextBoxFor(m => m.EditableGuest.PlusOneName)</td>
</tr>
<tr>
<td>RSVP Response:</td>
<td>@Html.DropDownListFor(m => m.EditableGuest.Response, new List<SelectListItem> { new SelectListItem { Value = "Accept with Pleasure", Text = "Accept with Pleasure" }, new SelectListItem { Value = "Decline with Regret", Text = "Decline with Regret" } })</td>
</tr>
<tr>
<td><button class="btnSubmitRSVP" type="submit"> @(Model.EditableGuest.GuestID > 0? "Update": "Add") </button></td>
<td></td>
</tr>
</table>
</div>
</div>
}
We cannot figure out why the model is being passed and not the viewmodel? My code doesn't refer to the model? Any help would be much appreciated!
解决方案
推荐阅读
- powershell - 通过powershell访问共享的office365邮箱
- ssrs-2012 - SSRS 2012 报告中未出现水平滚动条
- c# - 从 C# 在 microsoft word 中插入对象列表
- tensorflow - 如何使用 Tensorflow 将张量的每一列乘以另一个张量的所有列?
- java - 内存缓存 (CaffeineCache) 能否在应用程序重新启动后继续存在?
- java - org.springframework.web.client.HttpClientErrorException:404 未找到
- apache - Apache2 + Symfony4 + 子文件夹 = 问题
- python - 没有使用 python 3.6 和 apache2 的名为 celery 的模块
- python - 将 Numpy 导入 Sublime Text 3
- ruby-on-rails - Rails 聊天应用程序的 FAYE_URL 值应该是多少?