I want to show my create view into my main view which is index. So I take a partial view which is _Create.cshtml
. But after calling the partial view, I get an InvalidOperationException
.
This is my index.cshtml
:
@model IEnumerable<CommercialERP.Models.SelectList.BuyerSelectList>
<section class="content" style="display: block;">
<div class="container-fluid">
<div>
<div class="card card-primary card-outline card-outline-tabs">
<div class="card-header p-0 border-bottom-0">
<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" role="tab" aria-controls="home" aria-selected="true">Create</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" role="tab" aria-controls="profile" aria-selected="false">Details</a>
</li>
</ul>
</div>
<div class="card-body">
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<partial name="~/Areas/Admin/Views/BuyerSelectList/_Create.cshtml"/>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<table class="table table-bordered table-hover table-sm align-middle m-0" id="header">
<tr class="m-0" style="text-align:center;background-color: #17A2B8">
<th width="20%">
@Html.DisplayNameFor(model => model.Buyer)
</th>
<th></th>
<th></th>
</tr>
@foreach (var BuyerSelectList in Model)
{
<tr class="m-0">
<td>
@Html.DisplayFor(modelItem => BuyerSelectList.Buyer)
</td>
<td>
<div class="btn-group mx-auto" role="group" style="width:100%">
<a asp-controller="BuyerSelectList" asp-action="Edit" asp-route-id="@BuyerSelectList.Id" class="btn mx-2" style="background-color: #17A2B8">
<i class="bi bi-pencil-square"></i> Edit
</a>
</div>
</td>
<td>
<div class="btn-group" role="group" style="width:100%">
<a asp-controller="BuyerSelectList" asp-action="Delete" asp-route-id="@BuyerSelectList.Id" class="btn btn-danger mx-2">
<i class="bi bi-trash"></i> Delete
</a>
</div>
</td>
</tr>
}
</table>
</div>
</div>
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
And here is _create.cshtml
:
@model CommercialERP.Models.SelectList.BuyerSelectList
<form asp-action="Create">
<div class="row">
<div class="form-group">
<label asp-for="Buyer" class="control-label"></label>
<input asp-for="Buyer" class="form-control" />
<span asp-validation-for="Buyer" class="text-danger"></span>
</div>
<div class="form-group text-end m-0" style="border: 1px solid #99CCFF; height: 40px; width:100%; padding: 2px; background-color: #17A2B8">
<input type="submit" value="Create" class="btn btn-success btn-sm" style="background-color: #5E5A80; width: 160px;" />
</div>
</div>
</form>
This is my BuyerSelectListController
:
namespace CommercialERPWeb.Areas.Admin.Controllers.SelectList
{
[Area("Admin")]
public class BuyerSelectListController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public BuyerSelectListController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
IEnumerable<BuyerSelectList> objBuyerSelectListList = _unitOfWork.Buyer.GetAll();
return View(objBuyerSelectListList);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(BuyerSelectList obj)
{
if (ModelState.IsValid)
{
_unitOfWork.Buyer.Add(obj);
_unitOfWork.Save();
TempData["success"] = "Row Created Successfully!";
return RedirectToAction("Index");
}
return View(obj);
}
public IActionResult Edit(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var objBuyerSelectListList = _unitOfWork.Buyer.GetFirstOrDefault(c => c.Id == id);
if (objBuyerSelectListList == null)
{
return NotFound();
}
return View(objBuyerSelectListList);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(BuyerSelectList obj)
{
if (ModelState.IsValid)
{
_unitOfWork.Buyer.Update(obj);
_unitOfWork.Save();
TempData["success"] = "Row Edited Successfully!";
return RedirectToAction("Index");
}
return View(obj);
}
public IActionResult Delete(int? id)
{
if (id == null || id == 0)
{
return NotFound();
}
var objBuyerSelectListList = _unitOfWork.Buyer.GetFirstOrDefault(c => c.Id == id);
if (objBuyerSelectListList == null)
{
return NotFound();
}
return View(objBuyerSelectListList);
}
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public IActionResult DeletePOST(int? id)
{
var obj = _unitOfWork.Buyer.GetFirstOrDefault(c => c.Id == id);
if (obj == null)
{
return NotFound();
}
_unitOfWork.Buyer.Remove(obj);
_unitOfWork.Save();
TempData["success"] = "Row deleted successfully!";
return RedirectToAction("Index");
}
}
}
The error I get is:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[CommercialERP.Models.SelectList.BuyerSelectList]', but this ViewDataDictionary instance requires a model item of type 'CommercialERP.Models.SelectList.BuyerSelectList'.
Please help me solve this.