Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a partial view and add this in "Create.chtml" in my project which will display all area from database in create page. Everything(Insert, Update,Delete) works fine but when i click on the create button without select an item from dropdownlist it generate an error.

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


Partial View
HTML
@model IEnumerable<NHPractice2.Models.Area>
@using NHPractice2.Models

@{
    WebGrid grdArea = new WebGrid(Model, canPage: true, rowsPerPage: 10, defaultSort: "Name");
}

@grdArea.GetHtml(tableStyle: "Grid", headerStyle: "Header", alternatingRowStyle: "altRow", columns: grdArea.Columns(
    grdArea.Column("Sl No.", format: item => item.WebGrid.Rows.IndexOf(item) + 1, style: "serialCol"),
    grdArea.Column("Area", format: item => item.Name),
    grdArea.Column("City", format: item => item.CityName),
    grdArea.Column("Action", format: @<text>@Html.ActionLink("Edit", "Edit", new { id = item.ID }, new { onclick = "return confirm('Are sure wants to edit?');" }) | @Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { onclick = "return confirm('Are sure wants to delete?');" })</text>)

        )
           )


Controller Code
C#
[ChildActionOnly]
public ActionResult AreaList()
{            
  IEnumerable<Area> areas = AreaRepo.ShowAreaList(1, 0, "", 0);
  return PartialView("AreaList",areas);
}


Code from Create.chtml
HTML
@Html.Action("AreaList", new List<NHPractice2.Models.Area> { new NHPractice2.Models.Area() });


What I have tried:

I tried several times to solve it but same result.
Posted
Updated 7-Mar-16 21:45pm

1 solution

Error occurs due to model partial view and sending model to partial view.Try List like @model List<NHPractice2.Models.Area> instead of @model IEnumerable<NHPractice2.Models.Area>. Try with below steps:

In PartialView
Razor
@model List<NHPractice2.Models.Area>
@using NHPractice2.Models
 
@{
    WebGrid grdArea = new WebGrid(Model, canPage: true, rowsPerPage: 10, defaultSort: "Name");
}

In Controller
C#
[ChildActionOnly]
public ActionResult AreaList()
{            
  List<Area> areas = (AreaRepo.ShowAreaList(1, 0, "", 0)).ToList();
  return PartialView("AreaList", areas);
}

In view(to call partial view)
Razor
@Html.Action("AreaList", "ControllerName");
 
Share this answer
 
v4
Comments
Sharp10 8-Mar-16 15:47pm    
Thank for your response. If i use @model NHPractice2.Models.Area instead of @model IEnumerable<nhpractice2.models.area>, following error happens.

The best overloaded method match for System.Web.Helpers.WebGrid.WebGrid(System.Web.HttpContextBase, string, int, bool, bool, string, string, string, string, string, string, string)' has some invalid arguments

cannot convert from 'NHPractice2.Models.Area' to 'System.Web.HttpContextBase'
[no name] 8-Mar-16 23:54pm    
Updated code. Can you retry.
Sharp10 11-Mar-16 15:28pm    
Same result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900