【问题标题】:InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'UltraTel.Models.ViewModels.FRMultiViewModel'InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“UltraTel.Models.ViewModels.FRMultiViewModel”
【发布时间】:2020-01-12 07:18:30
【问题描述】:

我使用的是 Asp.Net core 2.2,我尝试在视图中使用部分视图。 (部分视图包括一些数据的列表)。我的代码一切正常,但我不记得我做了什么改变给了我这个问题。 我实际上已经阅读了这个QUESTION,但不幸的是我无法解决我的问题。感谢所有回复。

错误:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'UltraTel.Models.ViewModels.FRMultiViewModel', but this ViewDataDictionary instance requires a model item of type 'UltraTel.Models.Extra'.


这是我的观点:

@model UltraTel.Models.ViewModels.FRMultiViewModel
@{
    ViewData["Title"] = "title";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@addTagHelper * , Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper * , UltraTel*@

 <div class="container RoomsContainer">
        <div id="ChooseRoom"></div>
        @Html.Partial("_Room", Model.AllRooms)
        <div class="ExtrasContainer">
            @Html.Partial("_extra", Model.AllExtras)
        </div>
    </div>

这是上述视图所依赖的模型:

namespace UltraTel.Models.ViewModels
{
    public class FRMultiViewModel
    {
        public List<Room> AllRooms { get; set; }
        public List<Extra> AllExtras { get; set; }
    }
}

这是我的第一个局部视图及其模型:

@model IEnumerable<UltraTel.Models.Room>
@foreach (var room in Model)
{
      <input value="@room.Id" />
      <img src="@ViewBag.RootPath@room.RoomImageOne">
      <p>@room.Name</p>
      <input value="@room.Capacity" />
      <input value="@room.CostForOneNight" />
      <p>@room.NumberOfSingleBed</p>
      .
      .
      .
}


namespace UltraTel.Models
{
    public class Room
    {
         public int Id { get; set; }
         public string RoomImageOne { get; set; }
         public string Name { get; set; }
         public int Capacity { get; set; }
         public int CostForOneNight { get; set; }
         public int NumberOfSingleBed{ get; set; }
         .
         .
         .
     }
 }

这是我的第二个部分视图及其模型:

我认为错误是因为这个:(或者可能是因为控制器):

@model IEnumerable<UltraTel.Models.Extra>

@foreach (var extra in Model)
{
    <div class="Extra col-lg-3 col-md-4 col-sm-6">
        <div class="ExtraContent">
            <div class="ExtraTitleContainer">
                <p class="ExtraTitle">@extra.ExtraName</p>
            </div>
            <div class="ExtraStatus">
                <input type="checkbox" class="ExtraInput" id="@extra.Id" /><label class="ExtraLabel"></label>
            </div>
            <div class="br"></div>
            <p class="ExtraExplanation">@extra.ExtraExplanation</p>
            <div class="ExtraCostingContainer">
                <p class="ExtraCostingText">ریال</p>
                <p class="ExtraCosting">@extra.CostOfExtra</p>
            </div>
        </div>
    </div>
}


namespace UltraTel.Models
{
    public class Extra
    {
        public int Id { get; set; }
        public string ExtraName { get; set; }
        public string CostOfExtra { get; set; }
        public bool ExtraEnabled { get; set; }
        public string ExtraExplanation { get; set; }
    }
}

最后是我的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UltraTel.Models;
using UltraTel.Models.ViewModels;

namespace UltraTel.Controllers
{
    public class FullReservationController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly IServiceProvider _serviceProvider;
        public FullReservationController(ApplicationDbContext context, IServiceProvider serviceProvider)
        {
            _context = context;
            _serviceProvider = serviceProvider;
        }


        [HttpGet]
        public IActionResult Index()
        {
            FRMultiViewModel model = new FRMultiViewModel();
            model.AllRooms = (from r in _context.rooms select r).ToList();
            model.AllExtras = (from e in _context.extras select e).ToList();
            ViewBag.RootPath = "/upload/RoomNorm/";
            return View(model);
        }


    }
}

【问题讨论】:

  • 好像您没有完全阅读我的问题。我说我读过那个问题。但我无法解决我的问题。 (由于他的问题与我的细微差别)
  • 您已经发布了一半的项目,但仍然不知何故错过了重要的部分。根据错误,有一个带有@model UltraTel.Models.Extra 的视图,它是那个 视图正在被提供FRMultiViewModel。您发布的没有满足这些条件的视图,因此无法判断发生了什么。找到上述视图并弄清楚它是如何被合并的。
  • 我的项目视图中有一个部分以 @model UltraTel.Models.Extra 为基础。我在我的问题中提出了这种片面观点。还有一个视图可以输入 FRMultiViewModel 。我把所有的事情都放在了我的问题中。告诉我什么不是我的问题。所以我会再发一次
  • 没有。那部分使用IEnumerable&lt;Extra&gt;。错误只是说Extra。这里有另一种观点。

标签: asp.net asp.net-core unhandled-exception


【解决方案1】:

我发现我的问题是因为在“共享”目录中创建了部分视图。 该部分视图的名称是“_Login”,其中包含以下代码:

@model UltraTel.Models.ViewModels.AddUserViewModel
@using UltraTel.PublicModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, WarOfWords
<form class="AuthForm">
    <div class="AuthFormInputContainer">
        <input type="text" placeholder="Email" class="AuthFormInput" />
        <i class="fas fa-envelope AuthFormInputIco"></i>
    </div>
    <div class="AuthFormInputContainer">
        <input type="password" placeholder="Pass" class="AuthFormInput" />
        <i class="fas fa-unlock AuthFormInputIco"></i>
    </div>
    <br />
    <br />
    <div class="AuthButtonContainer">
        <input type="button" value="Forget your pass?" class="AuthFormButton v2" />
        <a href="#LoginComesOut" class="AuthFormButton v3">انصراف</a>
        <input type="button" value="ورود" class="AuthFormButton v1" />
    </div>
</form>

这个局部视图在“_Layout”中使用,我的主视图正在使用它。 (主要观点:我在问题中提出的观点相同——即使用部分观点)


此外,'_Login' 局部视图还没有控制器。错误是否可能是因为我没有任何用于“_Login”的控制器?还是有其他原因?

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2020-05-13
    • 2020-05-10
    • 1970-01-01
    • 2022-08-03
    • 2022-06-27
    • 2022-01-19
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多