The model item passed into the dictionary is of type .. but this dictionary requires a model item of type











up vote
40
down vote

favorite
10












This question and community wiki answer has been added to assist in closing out numerous unanswered questions as discussed in this meta post.





I have some code and when it executes, it throws an exception saying:




The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo




What does this mean, and how do I fix it?










share|improve this question
























  • I got this error when using return View instead of return PartialView so that's something to just check
    – Richard Housham
    Jul 5 at 15:19















up vote
40
down vote

favorite
10












This question and community wiki answer has been added to assist in closing out numerous unanswered questions as discussed in this meta post.





I have some code and when it executes, it throws an exception saying:




The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo




What does this mean, and how do I fix it?










share|improve this question
























  • I got this error when using return View instead of return PartialView so that's something to just check
    – Richard Housham
    Jul 5 at 15:19













up vote
40
down vote

favorite
10









up vote
40
down vote

favorite
10






10





This question and community wiki answer has been added to assist in closing out numerous unanswered questions as discussed in this meta post.





I have some code and when it executes, it throws an exception saying:




The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo




What does this mean, and how do I fix it?










share|improve this question















This question and community wiki answer has been added to assist in closing out numerous unanswered questions as discussed in this meta post.





I have some code and when it executes, it throws an exception saying:




The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo




What does this mean, and how do I fix it?







c# asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 '17 at 9:16









Community

11




11










asked Nov 2 '16 at 6:19







user3559349



















  • I got this error when using return View instead of return PartialView so that's something to just check
    – Richard Housham
    Jul 5 at 15:19


















  • I got this error when using return View instead of return PartialView so that's something to just check
    – Richard Housham
    Jul 5 at 15:19
















I got this error when using return View instead of return PartialView so that's something to just check
– Richard Housham
Jul 5 at 15:19




I got this error when using return View instead of return PartialView so that's something to just check
– Richard Housham
Jul 5 at 15:19












4 Answers
4






active

oldest

votes

















up vote
51
down vote



accepted










The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).



The error can be caused by



Passing the wrong model from a controller method to a view (or partial view)



Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view





var model = db.Foos.Select(x => new
{
ID = x.ID,
Name = x.Name
};
return View(model); // passes an anonymous object to a view declared with @model Foo


or passing a collection of objects to a view that expect a single object



var model = db.Foos.Where(x => x.ID == id);
return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo


The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.



Passing the wrong model from a view to a partial view



Given the following model



public class Foo
{
public Bar MyBar { get; set; }
}


and a main view declared with @model Foo and a partial view declared with @model Bar, then



Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
return View(model);


will return the correct model to the main view. However the exception will be thrown if the view includes



@Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }


By default, the model passed to the partial view is the model declared in the main view and you need to use



@Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }


to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be



@Html.Partial("_Bar", new Bar())


Declaring a model in a layout



If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.



If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.






share|improve this answer



















  • 12




    +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
    – Smix
    Mar 8 at 6:48


















up vote
3
down vote













Observe if the view has the model required:



View



@model IEnumerable<WFAccess.Models.ViewModels.SiteViewModel>

<div class="row">
<table class="table table-striped table-hover table-width-custom">
<thead>
<tr>
....


Controller



 [HttpGet]
public ActionResult ListItems()
{
SiteStore site = new SiteStore();
site.GetSites();

IEnumerable<SiteViewModel> sites =
site.SitesList.Select(s => new SiteViewModel
{
Id = s.Id,
Type = s.Type
});

return PartialView("_ListItems", sites);
}


In my case I Use a partial view but runs in normal views






share|improve this answer























  • What does this have to do with the question?
    – user3559349
    Nov 17 '17 at 20:45






  • 2




    @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
    – CelzioBR
    Nov 21 '17 at 13:37




















up vote
3
down vote













This question already has a great answer, but I ran into the same error, in a different scenario: displaying a List in an EditorTemplate.



I have a model like this:



public class Foo
{
public string FooName { get; set; }
public List<Bar> Bars { get; set; }
}

public class Bar
{
public string BarName { get; set; }
}


And this is my main view:



@model Foo

@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
@Html.EditorFor(m => m.Bars)


And this is my Bar EditorTemplate (Bar.cshtml)



@model List<Bar>

<div class="some-style">
@foreach (var item in Model)
{
<label>@item.BarName</label>
}
</div>


And I got this error:




The model item passed into the dictionary is of type 'Bar', but this
dictionary requires a model item of type
'System.Collections.Generic.List`1[Bar]






The reason for this error is that EditorFor already iterates the List for you, so if you pass a collection to it, it would display the editor template once for each item in the collection.



This is how I fixed this problem:



Brought the styles outside of the editor template, and into the main view:



@model Foo

@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
<div class="some-style">
@Html.EditorFor(m => m.Bars)
</div>


And changed the EditorTemplate (Bar.cshtml) to this:



@model Bar

<label>@Model.BarName</label>





share|improve this answer






























    up vote
    -1
    down vote













    This error simply means that you want single object in view and you are passing list of object.
    You can solve this error by adding .FirstOrDefault() after ToList().



    error



      studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();


    solved:



    var studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();.FirstOrDefault()





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40373595%2fthe-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown
























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      51
      down vote



      accepted










      The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).



      The error can be caused by



      Passing the wrong model from a controller method to a view (or partial view)



      Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view





      var model = db.Foos.Select(x => new
      {
      ID = x.ID,
      Name = x.Name
      };
      return View(model); // passes an anonymous object to a view declared with @model Foo


      or passing a collection of objects to a view that expect a single object



      var model = db.Foos.Where(x => x.ID == id);
      return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo


      The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.



      Passing the wrong model from a view to a partial view



      Given the following model



      public class Foo
      {
      public Bar MyBar { get; set; }
      }


      and a main view declared with @model Foo and a partial view declared with @model Bar, then



      Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
      return View(model);


      will return the correct model to the main view. However the exception will be thrown if the view includes



      @Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }


      By default, the model passed to the partial view is the model declared in the main view and you need to use



      @Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }


      to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be



      @Html.Partial("_Bar", new Bar())


      Declaring a model in a layout



      If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.



      If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.






      share|improve this answer



















      • 12




        +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
        – Smix
        Mar 8 at 6:48















      up vote
      51
      down vote



      accepted










      The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).



      The error can be caused by



      Passing the wrong model from a controller method to a view (or partial view)



      Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view





      var model = db.Foos.Select(x => new
      {
      ID = x.ID,
      Name = x.Name
      };
      return View(model); // passes an anonymous object to a view declared with @model Foo


      or passing a collection of objects to a view that expect a single object



      var model = db.Foos.Where(x => x.ID == id);
      return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo


      The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.



      Passing the wrong model from a view to a partial view



      Given the following model



      public class Foo
      {
      public Bar MyBar { get; set; }
      }


      and a main view declared with @model Foo and a partial view declared with @model Bar, then



      Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
      return View(model);


      will return the correct model to the main view. However the exception will be thrown if the view includes



      @Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }


      By default, the model passed to the partial view is the model declared in the main view and you need to use



      @Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }


      to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be



      @Html.Partial("_Bar", new Bar())


      Declaring a model in a layout



      If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.



      If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.






      share|improve this answer



















      • 12




        +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
        – Smix
        Mar 8 at 6:48













      up vote
      51
      down vote



      accepted







      up vote
      51
      down vote



      accepted






      The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).



      The error can be caused by



      Passing the wrong model from a controller method to a view (or partial view)



      Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view





      var model = db.Foos.Select(x => new
      {
      ID = x.ID,
      Name = x.Name
      };
      return View(model); // passes an anonymous object to a view declared with @model Foo


      or passing a collection of objects to a view that expect a single object



      var model = db.Foos.Where(x => x.ID == id);
      return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo


      The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.



      Passing the wrong model from a view to a partial view



      Given the following model



      public class Foo
      {
      public Bar MyBar { get; set; }
      }


      and a main view declared with @model Foo and a partial view declared with @model Bar, then



      Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
      return View(model);


      will return the correct model to the main view. However the exception will be thrown if the view includes



      @Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }


      By default, the model passed to the partial view is the model declared in the main view and you need to use



      @Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }


      to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be



      @Html.Partial("_Bar", new Bar())


      Declaring a model in a layout



      If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.



      If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.






      share|improve this answer














      The error means that you're navigating to a view whose model is declared as typeof Foo (by using @model Foo), but you actually passed it a model which is typeof Bar (note the term dictionary is used because a model is passed to the view via a ViewDataDictionary).



      The error can be caused by



      Passing the wrong model from a controller method to a view (or partial view)



      Common examples include using a query that creates an anonymous object (or collection of anonymous objects) and passing it to the view





      var model = db.Foos.Select(x => new
      {
      ID = x.ID,
      Name = x.Name
      };
      return View(model); // passes an anonymous object to a view declared with @model Foo


      or passing a collection of objects to a view that expect a single object



      var model = db.Foos.Where(x => x.ID == id);
      return View(model); // passes IEnumerable<Foo> to a view declared with @model Foo


      The error can be easily identified at compile time by explicitly declaring the model type in the controller to match the model in the view rather than using var.



      Passing the wrong model from a view to a partial view



      Given the following model



      public class Foo
      {
      public Bar MyBar { get; set; }
      }


      and a main view declared with @model Foo and a partial view declared with @model Bar, then



      Foo model = db.Foos.Where(x => x.ID == id).Include(x => x.Bar).FirstOrDefault();
      return View(model);


      will return the correct model to the main view. However the exception will be thrown if the view includes



      @Html.Partial("_Bar") // or @{ Html.RenderPartial("_Bar"); }


      By default, the model passed to the partial view is the model declared in the main view and you need to use



      @Html.Partial("_Bar", Model.MyBar) // or @{ Html.RenderPartial("_Bar", Model.MyBar); }


      to pass the instance of Bar to the partial view. Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be



      @Html.Partial("_Bar", new Bar())


      Declaring a model in a layout



      If a layout file includes a model declaration, then all views that use that layout must declare the same model, or a model that derives from that model.



      If you want to include the html for a separate model in a Layout, then in the Layout, use @Html.Action(...) to call a [ChildActionOnly] method initializes that model and returns a partial view for it.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 27 '17 at 23:29


























      community wiki





      3 revs, 2 users 96%
      Stephen Muecke









      • 12




        +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
        – Smix
        Mar 8 at 6:48














      • 12




        +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
        – Smix
        Mar 8 at 6:48








      12




      12




      +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
      – Smix
      Mar 8 at 6:48




      +1 for "Note also that if the value of MyBar is null (has not been initialized), then by default Foo will be passed to the partial, in which case, it needs to be". Very important note.
      – Smix
      Mar 8 at 6:48












      up vote
      3
      down vote













      Observe if the view has the model required:



      View



      @model IEnumerable<WFAccess.Models.ViewModels.SiteViewModel>

      <div class="row">
      <table class="table table-striped table-hover table-width-custom">
      <thead>
      <tr>
      ....


      Controller



       [HttpGet]
      public ActionResult ListItems()
      {
      SiteStore site = new SiteStore();
      site.GetSites();

      IEnumerable<SiteViewModel> sites =
      site.SitesList.Select(s => new SiteViewModel
      {
      Id = s.Id,
      Type = s.Type
      });

      return PartialView("_ListItems", sites);
      }


      In my case I Use a partial view but runs in normal views






      share|improve this answer























      • What does this have to do with the question?
        – user3559349
        Nov 17 '17 at 20:45






      • 2




        @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
        – CelzioBR
        Nov 21 '17 at 13:37

















      up vote
      3
      down vote













      Observe if the view has the model required:



      View



      @model IEnumerable<WFAccess.Models.ViewModels.SiteViewModel>

      <div class="row">
      <table class="table table-striped table-hover table-width-custom">
      <thead>
      <tr>
      ....


      Controller



       [HttpGet]
      public ActionResult ListItems()
      {
      SiteStore site = new SiteStore();
      site.GetSites();

      IEnumerable<SiteViewModel> sites =
      site.SitesList.Select(s => new SiteViewModel
      {
      Id = s.Id,
      Type = s.Type
      });

      return PartialView("_ListItems", sites);
      }


      In my case I Use a partial view but runs in normal views






      share|improve this answer























      • What does this have to do with the question?
        – user3559349
        Nov 17 '17 at 20:45






      • 2




        @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
        – CelzioBR
        Nov 21 '17 at 13:37















      up vote
      3
      down vote










      up vote
      3
      down vote









      Observe if the view has the model required:



      View



      @model IEnumerable<WFAccess.Models.ViewModels.SiteViewModel>

      <div class="row">
      <table class="table table-striped table-hover table-width-custom">
      <thead>
      <tr>
      ....


      Controller



       [HttpGet]
      public ActionResult ListItems()
      {
      SiteStore site = new SiteStore();
      site.GetSites();

      IEnumerable<SiteViewModel> sites =
      site.SitesList.Select(s => new SiteViewModel
      {
      Id = s.Id,
      Type = s.Type
      });

      return PartialView("_ListItems", sites);
      }


      In my case I Use a partial view but runs in normal views






      share|improve this answer














      Observe if the view has the model required:



      View



      @model IEnumerable<WFAccess.Models.ViewModels.SiteViewModel>

      <div class="row">
      <table class="table table-striped table-hover table-width-custom">
      <thead>
      <tr>
      ....


      Controller



       [HttpGet]
      public ActionResult ListItems()
      {
      SiteStore site = new SiteStore();
      site.GetSites();

      IEnumerable<SiteViewModel> sites =
      site.SitesList.Select(s => new SiteViewModel
      {
      Id = s.Id,
      Type = s.Type
      });

      return PartialView("_ListItems", sites);
      }


      In my case I Use a partial view but runs in normal views







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 20 at 14:30

























      answered Nov 17 '17 at 18:46









      CelzioBR

      614




      614












      • What does this have to do with the question?
        – user3559349
        Nov 17 '17 at 20:45






      • 2




        @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
        – CelzioBR
        Nov 21 '17 at 13:37




















      • What does this have to do with the question?
        – user3559349
        Nov 17 '17 at 20:45






      • 2




        @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
        – CelzioBR
        Nov 21 '17 at 13:37


















      What does this have to do with the question?
      – user3559349
      Nov 17 '17 at 20:45




      What does this have to do with the question?
      – user3559349
      Nov 17 '17 at 20:45




      2




      2




      @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
      – CelzioBR
      Nov 21 '17 at 13:37






      @StephenMuecke this champs - The model item passed into the dictionary is of type Bar but this dictionary requires a model item of type Foo
      – CelzioBR
      Nov 21 '17 at 13:37












      up vote
      3
      down vote













      This question already has a great answer, but I ran into the same error, in a different scenario: displaying a List in an EditorTemplate.



      I have a model like this:



      public class Foo
      {
      public string FooName { get; set; }
      public List<Bar> Bars { get; set; }
      }

      public class Bar
      {
      public string BarName { get; set; }
      }


      And this is my main view:



      @model Foo

      @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
      @Html.EditorFor(m => m.Bars)


      And this is my Bar EditorTemplate (Bar.cshtml)



      @model List<Bar>

      <div class="some-style">
      @foreach (var item in Model)
      {
      <label>@item.BarName</label>
      }
      </div>


      And I got this error:




      The model item passed into the dictionary is of type 'Bar', but this
      dictionary requires a model item of type
      'System.Collections.Generic.List`1[Bar]






      The reason for this error is that EditorFor already iterates the List for you, so if you pass a collection to it, it would display the editor template once for each item in the collection.



      This is how I fixed this problem:



      Brought the styles outside of the editor template, and into the main view:



      @model Foo

      @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
      <div class="some-style">
      @Html.EditorFor(m => m.Bars)
      </div>


      And changed the EditorTemplate (Bar.cshtml) to this:



      @model Bar

      <label>@Model.BarName</label>





      share|improve this answer



























        up vote
        3
        down vote













        This question already has a great answer, but I ran into the same error, in a different scenario: displaying a List in an EditorTemplate.



        I have a model like this:



        public class Foo
        {
        public string FooName { get; set; }
        public List<Bar> Bars { get; set; }
        }

        public class Bar
        {
        public string BarName { get; set; }
        }


        And this is my main view:



        @model Foo

        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        @Html.EditorFor(m => m.Bars)


        And this is my Bar EditorTemplate (Bar.cshtml)



        @model List<Bar>

        <div class="some-style">
        @foreach (var item in Model)
        {
        <label>@item.BarName</label>
        }
        </div>


        And I got this error:




        The model item passed into the dictionary is of type 'Bar', but this
        dictionary requires a model item of type
        'System.Collections.Generic.List`1[Bar]






        The reason for this error is that EditorFor already iterates the List for you, so if you pass a collection to it, it would display the editor template once for each item in the collection.



        This is how I fixed this problem:



        Brought the styles outside of the editor template, and into the main view:



        @model Foo

        @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
        <div class="some-style">
        @Html.EditorFor(m => m.Bars)
        </div>


        And changed the EditorTemplate (Bar.cshtml) to this:



        @model Bar

        <label>@Model.BarName</label>





        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote









          This question already has a great answer, but I ran into the same error, in a different scenario: displaying a List in an EditorTemplate.



          I have a model like this:



          public class Foo
          {
          public string FooName { get; set; }
          public List<Bar> Bars { get; set; }
          }

          public class Bar
          {
          public string BarName { get; set; }
          }


          And this is my main view:



          @model Foo

          @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
          @Html.EditorFor(m => m.Bars)


          And this is my Bar EditorTemplate (Bar.cshtml)



          @model List<Bar>

          <div class="some-style">
          @foreach (var item in Model)
          {
          <label>@item.BarName</label>
          }
          </div>


          And I got this error:




          The model item passed into the dictionary is of type 'Bar', but this
          dictionary requires a model item of type
          'System.Collections.Generic.List`1[Bar]






          The reason for this error is that EditorFor already iterates the List for you, so if you pass a collection to it, it would display the editor template once for each item in the collection.



          This is how I fixed this problem:



          Brought the styles outside of the editor template, and into the main view:



          @model Foo

          @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
          <div class="some-style">
          @Html.EditorFor(m => m.Bars)
          </div>


          And changed the EditorTemplate (Bar.cshtml) to this:



          @model Bar

          <label>@Model.BarName</label>





          share|improve this answer














          This question already has a great answer, but I ran into the same error, in a different scenario: displaying a List in an EditorTemplate.



          I have a model like this:



          public class Foo
          {
          public string FooName { get; set; }
          public List<Bar> Bars { get; set; }
          }

          public class Bar
          {
          public string BarName { get; set; }
          }


          And this is my main view:



          @model Foo

          @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
          @Html.EditorFor(m => m.Bars)


          And this is my Bar EditorTemplate (Bar.cshtml)



          @model List<Bar>

          <div class="some-style">
          @foreach (var item in Model)
          {
          <label>@item.BarName</label>
          }
          </div>


          And I got this error:




          The model item passed into the dictionary is of type 'Bar', but this
          dictionary requires a model item of type
          'System.Collections.Generic.List`1[Bar]






          The reason for this error is that EditorFor already iterates the List for you, so if you pass a collection to it, it would display the editor template once for each item in the collection.



          This is how I fixed this problem:



          Brought the styles outside of the editor template, and into the main view:



          @model Foo

          @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
          <div class="some-style">
          @Html.EditorFor(m => m.Bars)
          </div>


          And changed the EditorTemplate (Bar.cshtml) to this:



          @model Bar

          <label>@Model.BarName</label>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 8 at 0:26

























          answered Jul 7 at 13:37









          Hooman

          3,0343730




          3,0343730






















              up vote
              -1
              down vote













              This error simply means that you want single object in view and you are passing list of object.
              You can solve this error by adding .FirstOrDefault() after ToList().



              error



                studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();


              solved:



              var studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();.FirstOrDefault()





              share|improve this answer

























                up vote
                -1
                down vote













                This error simply means that you want single object in view and you are passing list of object.
                You can solve this error by adding .FirstOrDefault() after ToList().



                error



                  studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();


                solved:



                var studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();.FirstOrDefault()





                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  This error simply means that you want single object in view and you are passing list of object.
                  You can solve this error by adding .FirstOrDefault() after ToList().



                  error



                    studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();


                  solved:



                  var studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();.FirstOrDefault()





                  share|improve this answer












                  This error simply means that you want single object in view and you are passing list of object.
                  You can solve this error by adding .FirstOrDefault() after ToList().



                  error



                    studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();


                  solved:



                  var studentenrollment = _plarep.GetStudentEnrollment(studentid).ToList();.FirstOrDefault()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 13 at 7:11









                  Hitanshi Mehta

                  13




                  13






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40373595%2fthe-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno