使用单个剃刀视图访问两个模型的问题(ASP.NET MVC3)

时间:2013-06-04 14:41:30

标签: asp.net asp.net-mvc asp.net-mvc-3 razor

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>

2 个答案:

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

你实例化两个空列表...

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

类似

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

答案 1 :(得分:1)

像这样修改你的Model Class

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

我还添加了文本“list”作为列表名称的前缀,以区别于类名,否则列表名和类名都相同。

好的现在你还必须像这样修改你的 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