【问题标题】:The model item passed into the ViewDataDictionary is of type Models but needs ViewModels传入 ViewDataDictionary 的模型项是 Models 类型,但需要 ViewModels
【发布时间】:2021-12-24 07:34:26
【问题描述】:

几天来,我一直在为一个作业解决这个错误,但我无法修复它,我已经将它展示给另一个人,我们很困惑可能是什么问题? “InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为‘FYP_RSVP_MGMT.Models.GuestList’,但此 ViewDataDictionary 实例需要‘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 模型

 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>

}

我们无法弄清楚为什么传递的是模型而不是视图模型?我的代码没有引用模型?任何帮助将不胜感激!

【问题讨论】:

  • CreateUpdate 在 else 子句中有 return View("Index", new GuestList());
  • @GuruStron 等等,你的意思是我应该取出 else 子句中的代码,并用 return View("Index", new GuestList()); ?
  • 我的意思是在 else 子句中你已经有了这个代码。
  • 它应该类似于return View("Index",new GuestListViewModel());
  • @GuruStron 你是对的!由于某种原因,我认为它是 中的 GuestList,而不是认为它是模型,谢谢您的帮助!

标签: c# asp.net-mvc model-view-controller


【解决方案1】:

我会说它来自这里:

您在else 中发送了一个GuestList,尽管您承诺会在页面顶部发送GuestListViewModel

【讨论】:

  • 就是这样!太感谢了!下一个麻烦是在视图中添加访客时让它进入我的数据库!
  • 请随意提出另一个问题;这里不乏智能 cookie :)
猜你喜欢
  • 2020-10-20
  • 2018-12-25
  • 1970-01-01
  • 2020-10-03
  • 2022-06-27
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
相关资源
最近更新 更多