1

View(Index.chtml) 在访问视图中的两个模型时返回 0 行。请看下面的代码。我是 ASP.NET 的新手,我还在学习。我尝试调试,发现表数据未正确传递。请帮忙

================================================================================
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>
4

2 回答 2

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

您正在实例化两个空列表...

你应该把一些东西放在你的清单上!

就像是

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}
};
于 2013-06-04T14:44:16.340 回答
1

像这样修改你的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 )

就这些。现在您将能够看到如下输出:

在此处输入图像描述

于 2013-06-05T10:20:57.327 回答