0

I'm using Asp.Net core 2.2, I try to use partial views in a view. (the partial views are includes a list of some data). every things was okay with my codes, but i can't remember what changes I made that gives me this problem. i actually had read this QUESTION but unfortunately i a can't solve my problem. Thanks for all replies.

ERROR:

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'.


Here is my View:

@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>

Here is the model that the above view feeds on:

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

Here is my first partial view and its model:

@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; }
         .
         .
         .
     }
 }

Here is my second partial view an its model:

I think the error is because of this: (or maybe because of controller):

@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; }
    }
}

And finally here is my controller:

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);
        }


    }
}
  • Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) – Pritesh Sep 10 '19 at 13:57
  • As if you didn't read my question completely. I said I had read that question. But I couldn't solve my problem. (Due to the small differences that his question has with me) – HamidReza Ghafouri Sep 10 '19 at 14:01
  • You've posted half your project but still somehow missed the important bit. Based on the error, there's a view with `@model UltraTel.Models.Extra`, and it's *that* view that is being fed `FRMultiViewModel`. There's no view that you've posted that satisfies those conditions, so there's no way to tell what's going on. Find the said view and figure out how that's being incorporated. – Chris Pratt Sep 10 '19 at 14:04
  • There is a partial in my project view that feeds on `@model UltraTel.Models.Extra` . I putted that partial view in my question. and also there is a view that feeds in `FRMultiViewModel` . i have putted every thing in my question. tell me what is not in my question. so i will post that again – HamidReza Ghafouri Sep 10 '19 at 14:13
  • No. That partial use `IEnumerable`. The error says just `Extra`. There's another view in the mix here somewhere. – Chris Pratt Sep 10 '19 at 14:22
  • please read my second question in this page – HamidReza Ghafouri Sep 10 '19 at 14:40
  • Don't use partial views like this. MVC has a specific feature designed to solve this problem called Editor and ViewTemplates. See https://exceptionnotfound.net/asp-net-mvc-demystified-display-and-editor-templates/ – Erik Funkenbusch Sep 10 '19 at 14:45
  • Thank you, i will read that – HamidReza Ghafouri Sep 10 '19 at 14:52

1 Answers1

1

I found that my problem is because of creating a partial view in 'Shared' directory. The name of that partial view is '_Login' and its contains of this codes:

@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>

This partial view is using in '_Layout', which my main view is using that. (main view: that same view that i putted in my question --that is using partial views)


In addition, the '_Login' partial view, has no controller yet. Is it possible that the Error is because I don't have any controller for '_Login'? Or is there another reason?