【问题标题】:Issue with a single razor view accessing two models (ASP.NET MVC3)访问两个模型的单个剃刀视图的问题 (ASP.NET MVC3)
【发布时间】:2013-06-04 14:41:29
【问题描述】:

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>

【问题讨论】:

  • 那是因为你从来没有把任何东西放在列表中。
  • 您看到 0 行的原因是因为您正在创建两个空列表。你在期待什么,想做什么?
  • 我正在尝试使用通过访问两个模型生成的数据创建一个视图

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


【解决方案1】:
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}
};

【讨论】:

  • 感谢您的回复。我不知道如何填写这些清单。比如上面的代码中,TABLE_CODES 和 TABLE_ORGANIZATIONS 是有真实数据的表
  • 好吧。它们是您的表格的“对象”表示。所以你肯定有办法用数据填充这些对象,不是吗?
  • 是的,我可以从您对填充这些对象的回复中理解这一点。我会试一试。谢谢拉斐尔!
  • 我使用了您建议的第二种方法来填充列表: List temp_codes= new List { new TABLE_CODES() }; //我希望将整个表“TABLE_CODES”数据填充到 temp_codes // 这没有帮助我仍然看到没有值的表
【解决方案2】:

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

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

【讨论】:

  • 感谢 Jitender。最好的部分是我从拉斐尔的回应中学到了我所缺少的东西,并且我从你的那里学到了应该如何处理和解决问题。谢谢!
  • 欢迎Archer,很高兴我的回答也对您有所帮助。 :)
猜你喜欢
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
相关资源
最近更新 更多