sunskin
sunskin

Reputation: 1680

Issue with a single razor view accessing two models (ASP.NET MVC3)

View(Index.chtml) is returning 0 rows while accessing two models in the view. Please see the code below. I am new to ASP.NET and I am still learning. I tried to debug and I see the table data is not being passed to correctly. Please help

================================================================================
Controller: (OrganizationCodesController.cs)
================================================================================
namespace MvcProject.Controllers
{
    public class OrganizationCodesController : Controller
    {
        //
        // GET: /OrganizationCodes/

        public ActionResult Index()
        {
            List<TABLE_CODES> temp_codes = new List<TABLE_CODES>();
            List<TABLE_ORGANIZATIONS> temp_organizations = new List<TABLE_ORGANIZATIONS>();

            var viewModel = new OrganizationCodesModel(temp_codes, temp_organizations);
            return View(viewModel);

        }
    }       
============================================================================
Model: (OrganizationCodesModel.cs)
============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace MvcProject.Models
{


    public class OrganizationCodesModel 
    {
        public List<TABLE_CODES> TABLE_CODES { get; set; }
        public List<TABLE_ORGANIZATIONS> TABLE_CODES { get; set; }

        public OrganizationCodesModel(List<TABLE_CODES> _codes, List<TABLE_ORGANIZATIONS> _organizations)
        {
            TABLE_CODES = _codes;
            TABLE_ORGANIZATIONS = _organizations;
        }   
    }
}
========================================================================
View: (Index.chtml)
========================================================================
@model MvcProject.Models.OrganizationCodesModel

<table>
<thead>
<tr>
        <th>
            ORGANIZATION_NAME
        </th>
        <th>
            RANK
        </th>
        <th>
            LEVEL
        </th>
</thead>    
<tbody> 
@foreach (var item in Model.TABLE_CODES) {
    <tr>
        <td>
        @foreach (var item_1 in Model.TABLE_ORGANIZATIONS)
        {
            if (item.LOCATION == item_1.ID)
            {
            @item1.NAME
                break;
            }
        }    
        </td>
        <td>
        @item.RANK
        </td>
        <td>
        @item.LEVEL
        </td>
    </tr>
}   
</tbody>
</table>

Upvotes: 1

Views: 593

Answers (2)

Jitender Kumar
Jitender Kumar

Reputation: 2587

Modiy your Model Class like this:

public class OrganizationCodesModel
{
    public List<TABLE_CODES> listTABLE_CODES { get; set; }
    public List<TABLE_ORGANIZATIONS> listTABLE_ORGANIZATIONS { get; set; }
}

I have also added text "list" as prefix to the name of the list to distinguish it from the class name otherwise both list name and class name are same.

Ok Now you have to also modify your Index action method like this:

 public ActionResult Index()
    {
        OrganizationCodesModel model = new OrganizationCodesModel();

        List<TABLE_CODES>listCodes = new List<TABLE_CODES> {
          new TABLE_CODES {LOCATION = 1, RANK = 1, LEVEL  =1},
          new TABLE_CODES{LOCATION = 2, RANK = 3, LEVEL = 12345}
        };
        List<TABLE_ORGANIZATIONS> listOrganisation = new List<TABLE_ORGANIZATIONS> {
          new TABLE_ORGANIZATIONS {ID = 1,NAME="ABC"},
          new TABLE_ORGANIZATIONS{ID = 2,NAME="XYZ"}
        };

        model.ListTABLE_CODES = listCodes;
        model.ListTABLE_ORGANIZATIONS = listOrganisation;
        return View(model);
    }

and in your View just replace your List name like this:

@foreach (var item in Model.listTABLE_CODES )
@foreach (var item_1 in Model.listTABLE_ORGANIZATIONS )

That is all. Now you will be able to see your output like this:

enter image description here

Upvotes: 1

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

List<TABLE_CODES> temp_codes = new List<TABLE_CODES>();
List<TABLE_ORGANIZATIONS> temp_organizations = new List<TABLE_ORGANIZATIONS>();

var viewModel = new OrganizationCodesModel(temp_codes, temp_organizations);

your instanciating two empty lists...

you should put something in your lists !

something like

List<TABLE_CODES> temp_codes = GetTempCodesFromSomewhere();

or

List<TABLE_CODES> temp_codes = new List<TABLE_CODES> {
   new TABLE_CODES {LOCATION = 1, RANK = 1, LEVEL  =1},
   new TABLE_CODES{LOCATION = 2, RANK = 3, LEVEL = 12345}
};

Upvotes: 2

Related Questions