0

i need show all category in view . i used this code for show all :

Category = Tabel name of Database

public List<Category> GetAllCategory()
    {
        return _repository.GetAll();
    }

/****************************************/

public ActionResult ListCategory()
    {
        return View(_CategoryService.GetAllCategory());
    }

/*****************************************/

i create model CategoryModel .

 public class CategoryModel 
{
    [Display(Name ="کد دسته")]
    public int CatID { get; set; }
    [Display(Name = " نام دسته")]
    public string CategoryName { get; set; }
    [Display(Name = " سر دسته")]
    public int ParentID { get; set; }
}

/**********************************/

and in view i use this :

@model IEnumerable<Amene.Models.CategoryModel>

but it show me this error :

The model item passed into the dictionary is of type 'System.Collections.Generic.List1[DA.Data.DB.Category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Amene.Models.CategoryModel]'.

o know i must user table name in view Category but i need use CategoryModel .

how can i do this ?

Kianoush
  • 47
  • 10
  • Your method returns `List` and `Category` is not the same as `CategoryModel` If you want `CategoryModel`, then generate a collection of that type. –  Feb 13 '17 at 09:15
  • @StephenMuecke how can i do this ? – Kianoush Feb 13 '17 at 09:26
  • `_CategoryService.GetAllCategory().Select(x => new CategoryModel { CatID = x.CatID, CategoryName = x.CategoryName, .... })` assuming the property names of `Category` are the same as the property names of `CategoryModel` –  Feb 13 '17 at 09:28
  • @StephenMuecke this problem not have any way without this ? – Kianoush Feb 13 '17 at 12:38
  • Of course not. You view expects `IEnumerable` so you need to give it `IEnumerable` –  Feb 13 '17 at 20:58

0 Answers0