传递到 ViewDataDictionary 的模型项是模型类型,但需要 ViewModels
The model item passed into the ViewDataDictionary is of type Models but needs ViewModels
为了一项作业,我已经为这个错误苦苦挣扎了几天,但我无法修复它,我已经将它展示给另一个人,我们对可能是什么问题感到困惑? “InvalidOperationException:传入 ViewDataDictionary 的模型项的类型为 'FYP_RSVP_MGMT.Models.GuestList',但此 ViewDataDictionary 实例需要类型为 'FYP_RSVP_MGMT.ViewModels.GuestListViewModel' 的模型项。”
访客列表控制器:
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; }
}
客人名单模型
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 - 表单显示位置
@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>
}
我们无法弄清楚为什么传递的是模型而不是视图模型?我的代码没有引用模型?任何帮助将不胜感激!
我会说它来自这里:
尽管您承诺会在页面顶部发送 GuestListViewModel
,但您还是在 else
中发送了 GuestList:
为了一项作业,我已经为这个错误苦苦挣扎了几天,但我无法修复它,我已经将它展示给另一个人,我们对可能是什么问题感到困惑? “InvalidOperationException:传入 ViewDataDictionary 的模型项的类型为 'FYP_RSVP_MGMT.Models.GuestList',但此 ViewDataDictionary 实例需要类型为 'FYP_RSVP_MGMT.ViewModels.GuestListViewModel' 的模型项。”
访客列表控制器:
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; }
}
客人名单模型
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 - 表单显示位置
@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>
}
我们无法弄清楚为什么传递的是模型而不是视图模型?我的代码没有引用模型?任何帮助将不胜感激!
我会说它来自这里:
尽管您承诺会在页面顶部发送 GuestListViewModel
,但您还是在 else
中发送了 GuestList: