1

I want to create a view page like below sketch. It has 3 sections .

Section 1: Table List - showing the list of available products

Section 2: Summary 1 - showing the 2 of newest launched products with short descriptions

Section 3: Summary 2 - showing the 3 of newest group discussion topics with short descriptions

enter image description here

For that I just followed this Tutorial

I just tried to create show the table as partial view initially.later I hope to add other two sections.

Designed model like this

public class DashboardViewModel
{
    public ProductTableViewModel ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}
public class ProductTableViewModel
{
    [Display(Name = "S.No")]
    public string Product_ID { get; set; }

    [Display(Name = "Product Title")]
    public string Product_Title_EN { get; set; }

    .....
}

for this I just created DAL like this

  public class DashboardData
    {
        public DBEntities db = new DBEntities();
        public ProductTableViewModel GetAllProducts()
        {
              var result = (from product in db.AB_Product
                            .....
                            select new ProductTableViewModel
                            {
                            .....     
                            }).ToList();

            return result;
        }

then I call it in controller like this .

    public ActionResult Dashboard()
    {

        ProductTableViewModel pdct = new ProductTableViewModel();
        DashboardViewModel puff = new DashboardViewModel();


        puff.ProductTable = pdct.GetAllProducts();

        return View(puff);    
    }

then I'm getting following errors

Cannot implicitly convert type System.Collections.Generic.List<project_name.Models.ProductTableViewModel>' to 'project_name.Models.ProductTableViewModel'

other error

'ProductTableViewModel' does not contain a definition for 'GetAllProducts' and no extension method 'GetAllProducts' accepting a first argument of type 'ProductTableViewModel'

4
  • You GetAllProducts() method appears to be returning a collection of ProductTableViewModel (not a single ProductTableViewModel). You method should be public List<ProductTableViewModel> GetAllProducts() and the property in your model should be public List<ProductTableViewModel> ProductTable; Commented Dec 2, 2015 at 9:00
  • @StephenMuecke once I change like that getting following error in controller method 'ProductTableViewModel' does not contain a definition for 'GetAllProducts' and no extension method 'GetAllProducts' accepting a first argument of type 'ProductTableViewModel' could be found Commented Dec 2, 2015 at 9:56
  • No it does not, but DashboardData does :). In anycase, your GetAllProducts() should not be inside a view model class. It should be a separate service. Commented Dec 2, 2015 at 10:00
  • And it would be a far better design to just have 3 separate action method that return partial view for each section, and then in the view use @Html.Action("FetchAvaliableProducts", "Products") etc Commented Dec 2, 2015 at 10:05

2 Answers 2

2

Change type of pdct ProductTableViewModel to DashboardData

DashboardData pdct = new DashboardData()

and

public class DashboardViewModel
{
    public List<ProductTableViewModel>  ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}
public List<ProductTableViewModel> GetAllProducts()
        {
              var result = (from product in db.AB_Product
                            .....
                            select new ProductTableViewModel
                            {
                            .....     
                            }).ToList();

            return result;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

I changed as you said but then getting following exception The model item passed into the dictionary is of type 'project_name.Models.DashboardViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[project_name.Models.ProductTableViewModel]'`
2

Here you are trying to return a List of ProductTableViewModels not a single item of ProductTableViewModel.So for first change the return type to List<ProductTableViewModel>

 public class DashboardData
 {
         public DBEntities db = new DBEntities();
         public List<ProductTableViewModel> GetAllProducts()
         {
               var result = (from product in db.AB_Product
                                .....
                             select new ProductTableViewModel
                             {
                             .....     
                             }).ToList();

               return result;
         }
 }

and then try to change your ViewModel as you want

public class DashboardViewModel
{
    public List<ProductTableViewModel> ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}

5 Comments

once I change like that getting following error in controller method 'ProductTableViewModel' does not contain a definition for 'GetAllProducts' and no extension method 'GetAllProducts' accepting a first argument of type 'ProductTableViewModel' could be found
@kez but your method is in DashboardData not in ProductTableViewModel
I cannot understand you said
@kez on which object you are trying to call the GetAllProducts method ?
I didn't create a object using relevant model class , I just want to return the var result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.