Click here to Skip to main content
15,309,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--Scenario
1)i have two class in model
2)I am creating list object of both the class and trying to populate dummy data.
3 probelm --> how to pass two list object in view. So that i can create two webgrid in one view


--Model
C#
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Tesco.RetalixTenNFR.MVC.Models
{
    public class SuccessFailureModel
    {

        public string Country { get; set; }
        public string FormatStoreType { get; set; }
        public int? Stores { get; set; }
        public int? Tills { get; set; }
        public int? Tokens { get; set; }
        public string OverallBrag { get; set; }


    }

    public class OverAllDmsBrag
    {

        public string OverAllDms { get; set; }
        public string OverallBragColor { get; set; }

    }


}



--------------------------controller-------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tesco.RetalixTenNFR.MVC.Models;

namespace Tesco.RetalixTenNFR.MVC.Controllers
{
    public class SuccessFailureController : Controller
    {
        //
        // GET: /SuccessFailure/
        public ActionResult Index()
        {
            List<SuccessFailureModel> SuccessFailureObjList = new List<SuccessFailureModel>();
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "UK", Stores = 257, Tills = 420, Tokens = 1200, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Format And Store Type", Stores =null, Tills = null, Tokens = null, OverallBrag = null });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Non Express", Stores = 30, Tills = 100, Tokens = 500, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "SuperStore", Stores = 25, Tills = 50, Tokens = 150, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Metro", Stores = 50, Tills = 48, Tokens = 75, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "HomePlus", Stores = 0, Tills = 200, Tokens = 2, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Express", Stores = 150, Tills = 220, Tokens = 473, OverallBrag = "RED" });

            List<OverAllDmsBrag> OverAllObj = new List<OverAllDmsBrag>();
            OverAllObj.Add(new OverAllDmsBrag { OverAllDms = "Over All DMS Brag", OverallBragColor = "RED" });
            return View(OverAllObj);
            //return View(SuccessFailureObjList);
        }

      
    } 
}


--in controller i am creating two list. How it is possible to pass two object to view in controller. getting one webgrid is simple as we need to pass one object.

--Kindly help me out as i am new to mvc
Posted

Simple Solution is use 'ViewModel' class. In this class add two List<type> properties and pass the viewmodel object to view.

XML
Public Class MyViewModel
{
    Public List<SuccessFailureModel> List1{get;set;}
    Public List<SuccessFailureModel> List2{get;set;}
}


namespace Tesco.RetalixTenNFR.MVC.Controllers
{
    public class SuccessFailureController : Controller
    {
        //
        // GET: /SuccessFailure/
        public ActionResult Index()
        {
            List<SuccessFailureModel> SuccessFailureObjList = new List<SuccessFailureModel>();
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "UK", Stores = 257, Tills = 420, Tokens = 1200, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Format And Store Type", Stores =null, Tills = null, Tokens = null, OverallBrag = null });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Non Express", Stores = 30, Tills = 100, Tokens = 500, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "SuperStore", Stores = 25, Tills = 50, Tokens = 150, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Metro", Stores = 50, Tills = 48, Tokens = 75, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "HomePlus", Stores = 0, Tills = 200, Tokens = 2, OverallBrag = "RED" });
            SuccessFailureObjList.Add(new SuccessFailureModel { FormatStoreType = "Express", Stores = 150, Tills = 220, Tokens = 473, OverallBrag = "RED" });

            List<OverAllDmsBrag> OverAllObj = new List<OverAllDmsBrag>();
            OverAllObj.Add(new OverAllDmsBrag { OverAllDms = "Over All DMS Brag", OverallBragColor = "RED" });
            MyViewModel model=new MyViewModel();
            model.List1=SuccessFailureObjList;
            model.List2=OverAllObj;
            return View(model);
            //return View(SuccessFailureObjList);
        }


    }
}
   
v2
Comments
anurag19289 19-Feb-14 7:38am
   
I am unable to do it. Can you help me giving me an example.
R-a-v-i-k-u-m-a-r 19-Feb-14 7:47am
   
Added code to solution
anurag19289 19-Feb-14 10:58am
   
I will try and get back to you
   

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