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>
那是因爲你從來沒有把在列表中任何東西。 – SLaks
您看到0行的原因是您正在創建兩個空列表。你期待什麼,試圖做什麼? – Kenneth
我正嘗試創建一個視圖,其中包含訪問兩個模型生成的數據 – sunskin