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