2013-06-04 58 views
1

View(Index.chtml)在訪問視圖中的兩個模型時返回0行。請參閱下面的代碼。我是ASP.NET新手,我仍在學習。我試圖調試,我看到表數據沒有正確傳遞。請幫助訪問兩個模型(ASP.NET MVC3)的單個剃鬚刀視圖的問題

================================================================================ 
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> 
+1

那是因爲你從來沒有把在列表中任何東西。 – SLaks

+0

您看到0行的原因是您正在創建兩個空列表。你期待什麼,試圖做什麼? – Kenneth

+0

我正嘗試創建一個視圖,其中包含訪問兩個模型生成的數據 – sunskin

回答

1

Modiy您Model Class這樣的:

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

我還添加文本 「清單」 作爲前綴列表的名稱將其與類名區分開來,否則列表名和類名相同。

現在好了,你也必須修改Index action method這樣的:

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); 
    } 

,並在您View只需更換你的列表名稱是這樣的:

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

這是所有。現在,您將能夠看到你這樣的輸出:

enter image description here

+0

感謝Jitender。最好的部分是我瞭解到我從拉斐爾的回答中所缺少的東西,並且我學會了如何通過你的方式來解決問題。謝謝! – sunskin

+0

歡迎阿切爾,我很高興我的回答也幫助你。 :) –

2
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); 

您instanciating兩個空單...

,你應該把你的名單東西!

List<TABLE_CODES> temp_codes = GetTempCodesFromSomewhere(); 

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} 
}; 
+0

感謝您的回覆。我不確定如何填寫這些列表。例如,在上面的代碼中,TABLE_CODES和TABLE_ORGANIZATIONS是具有實際數據 – sunskin

+1

的表。它們是表格的「對象」表示。所以你肯定有辦法用Data來填充這些對象,不是嗎? –

+0

是的,我可以從你對填充這些對象的迴應中理解。我會試一試。謝謝拉斐爾! – sunskin