InvalidOperationException:传递到ViewDataDictionary中的模型项的类型为'UltraTel.Models.ViewModels.FRMultiViewModel'

时间:2019-09-10 13:55:37

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

我正在使用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);
        }


    }
}

1 个答案:

答案 0 :(得分:0)

我发现我的问题是由于在“共享”目录中创建了局部视图。 该部分视图的名称为“ _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”的任何控制器?还是还有其他原因?