0

I got an error while I try to get to my view. I've tried without IEnumerable but it doesn't work as well.

Error: MVC: The model item passed into the dictionary is of type System.Int32 while this dictionary requires a type model element System.Collections.Generic.IEnumerable`1[Cinema.Models.Video].

@model IEnumerable<Cinema.Models.Video>
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

My controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Search()
    {
        CinemaEntities db = new CinemaEntities();
        return View(db.SearchVideo(""));
    }

    [HttpPost]
    public ActionResult Search(string VideoName)
    {
        CinemaEntities db = new CinemaEntities();
        return View(db.SearchVideo(VideoName));
    }
}

My SearchVideo

public virtual ObjectResult<Video_SearchVideo_Result1> Video_SearchVideo(string videoName)
{
    var videoNameParameter = videoName != null ?
        new ObjectParameter("VideoName", videoName) :
        new ObjectParameter("VideoName", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Video_SearchVideo_Result1>("Video_SearchVideo", videoNameParameter);
}

public virtual int SearchVideo(string videoName)
{
    var videoNameParameter = videoName != null ?
        new ObjectParameter("VideoName", videoName) :
        new ObjectParameter("VideoName", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SearchVideo", videoNameParameter);
}
mariie
  • 45
  • 1
  • 10
  • 1
    Please show update the question with the ECinema.Models.Video model – AlameerAshraf Dec 02 '17 at 12:10
  • 1
    `int SearchVideo` is being passed to a view `return View(db.SearchVideo(VideoName))` that expects `@model IEnumerable` just as the error message states. You need to match up what you return to the with what it (the view) expects to receive – Nkosi Dec 02 '17 at 12:17
  • but I've got that @model IEnumerable in my view – mariie Dec 02 '17 at 12:19

1 Answers1

1

SearchVideo returns an int as per your code:

public virtual int SearchVideo(string videoName)

You need to change that function to return IEnumerable<ECinema.Models.Video> instead.

The easiest way is likely to use LINQ rather than call the stored procedure. But if you really want to keep the stored procedure, you may be able to configure it to return your desired type.

mjwills
  • 23,389
  • 6
  • 40
  • 63