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












42














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 '18 at 15:19
















42














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 '18 at 15:19














42












42








42


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 '18 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 '18 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 '18 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 '18 at 15:19












3 Answers
3






active

oldest

votes


















54














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



















  • 13




    +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 '18 at 6:48



















3














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





















3














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























    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',
    autoActivateHeartbeat: false,
    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
























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    54














    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



















    • 13




      +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 '18 at 6:48
















    54














    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



















    • 13




      +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 '18 at 6:48














    54












    54








    54






    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









    • 13




      +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 '18 at 6:48














    • 13




      +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 '18 at 6:48








    13




    13




    +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 '18 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 '18 at 6:48













    3














    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


















    3














    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
















    3












    3








    3






    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 '18 at 14:30

























    answered Nov 17 '17 at 18:46









    CelzioBRCelzioBR

    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













    3














    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




























      3














      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


























        3












        3








        3






        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 '18 at 0:26

























        answered Jul 7 '18 at 13:37









        Hooman BahreiniHooman Bahreini

        3,2023730




        3,2023730






























            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

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo