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!
CodePudding user response:
I'd say it's coming from here:
You're sending a GuestList in the else
despite promising you'd send a GuestListViewModel
at the top of the page: