InvalidOperationException: Model item passed in the ViewDataDictionary is of type 'System.Object'

Viewed 280

I encountered the problem when I was creating a Create page. Which lead me to an error message which says:


Error Message: Error message


Im not entirely sure if my model name is correct. Below are my following codes:

Songs Controller:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using System.Data;
using System.Data.SqlClient;
using System;
using Garcia___MVC.Models;

namespace Garcia___MVC.Controllers
{
    public class SongsController : Controller
    {
        private const string ConnectionString =
              @"*";

        // GET: SongsController
        [HttpGet]
        public ActionResult Index()
        {
            DataTable dtbSongs = new DataTable();
            using(SqlConnection sqlCon = new SqlConnection(ConnectionString))
            {
                sqlCon.Open();
                SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Songs",sqlCon);
                sqlDa.Fill(dtbSongs);
            }
            return View(dtbSongs);
        }


        // GET: SongsController/Create
        [HttpGet]
        public ActionResult Create(object model)
        {
            return View(model);
        }

        // POST: SongsController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: SongsController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: SongsController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: SongsController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: SongsController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

Create Page that can't be accessed:

@model Garcia___MVC.Models.SongsModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>SongsModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="SongID" class="control-label"></label>
                <input asp-for="SongID" class="form-control" />
                <span asp-validation-for="SongID" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="SongTitle" class="control-label"></label>
                <input asp-for="SongTitle" class="form-control" />
                <span asp-validation-for="SongTitle" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Artists" class="control-label"></label>
                <input asp-for="Artists" class="form-control" />
                <span asp-validation-for="Artists" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Album" class="control-label"></label>
                <input asp-for="Album" class="form-control" />
                <span asp-validation-for="Album" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

SongsModel.cs

using System;

namespace Garcia___MVC.Models
{
    public class SongsModel
    {
        public int SongID { get; set; } 
        public string SongTitle { get; set; } 
        public string Artists { get; set; } 
        public string Album { get; set; }
    }
}

Would appreciate any advice or tips on how I can go about this. Will be very much appreciated.

I was expecting a page which you can create the Song title, Artists, Album, and Song ID.

1 Answers

The return type of the action in the songsController must be SongsModel. The type you specify at the top of the view page determines the return type. I'm sorry for my bad english.

Related