【问题标题】:Issue with using a list to populate a table in an MVC 5 View使用列表填充 MVC 5 视图中的表的问题
【发布时间】:2018-08-02 17:03:45
【问题描述】:

我的控制器中有以下代码:

public ActionResult Index(int Id)
    {
        Landbase _db = new Landbase();

        OwnerWorkingInterests workingInterests = new OwnerWorkingInterests();

        //Owner owner = new Owner();



        var query = (from wg in _db.WorkingInterestGroups
            join wi in _db.WorkingInterests on wg.Id equals wi.WorkingInterestGroupId
            join l in _db.Leases on wg.LeaseId equals l.Id
            where wi.OwnerId.Equals(Id)
            select new OwnerWorkingInterests()
            {
                LeaseId = l.Id,
                WorkingInterestAmount = wi.WorkingInterestAmount,
                WorkingInterestGroupName = wg.Name,
                ClientAlias = l.ClientAlias,
                Lessor = l.Lessor,
                Lessee = l.Lessee,
                VolDocNumber = l.VolumeDocumentNumber,
                County = l.County,
                District = l.District
            }).ToList();

        //List<string> OwnerWorkingInterest = query.ToList<string>();

        return View(query);
    }

我认为有以下代码:

        <div id="OwnerWorkingInterests" class="tab-pane fade">
        <h3>Working Interests</h3>
        <table class="table">
            <thead>
                <tr>
                    <td>Lease Id:</td>
                    <td>Working Int:</td>
                    <td>WI Group Name:</td>
                    <td>Alias:</td>
                    <td>Lessor:</td>
                    <td>Lessee:</td>
                    <td>VolPg:</td>
                    <td>County:</td>
                    <td>District0:</td>
                </tr>
            </thead>
            <tbody>
            @foreach (var owi in OwnerWorkingInterests)
            {
                <tr>
                    <td>@owi.LeaseId</td>
                    <td>@owi.WorkingInterestAmount</td>
                    <td>@owi.WorkingInterestGroupName</td>
                    <td>@owi.ClientAlias</td>
                    <td>@owi.Lessor</td>
                    <td>@owi.Lessee</td>
                    <td>@owi.VolDocNumber</td>
                    <td>@owi.County</td>
                    <td>@owi.District</td>
                </tr>
            }
            </tbody>
        </table>
    </div>

我认为这会用正确的信息填充表格

这是视图模型:

namespace LandPortal.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class WorkingInterest
{
    public int Id { get; set; }

    public int? OwnerId { get; set; }

    [Column("WorkingInterest")]
    public decimal? WorkingInterestAmount { get; set; }

    [StringLength(45)]
    public string CreateUser { get; set; }

    [StringLength(45)]
    public string ModifyUser { get; set; }

    public Guid? CreateUserId { get; set; }

    public Guid? ModifyUserId { get; set; }

    public DateTime? CreateDate { get; set; }

    public DateTime? ModifyDate { get; set; }

    public int? WorkingInterestGroupId { get; set; }

    public WorkingInterestGroup WorkingInterestGroup { get; set; }

    public decimal? ORRI { get; set; }

    public int? ORRIOwnerId { get; set; }

    public virtual Owner Owner { get; set; }


}

}

所以当我在调试器中运行它时,它会抛出一个非常模糊的错误。它实际上只是说错误:处理您的请求时发生错误。所以我假设列表正在填充,但在视图中的 foreach 中不起作用。在这一点上我可能是错的。

这里是视图的模型指令

@using LandPortal.Models
@using LandPortal.ViewModels
@using Microsoft.Ajax.Utilities
@model LandPortal.Models.Owner

【问题讨论】:

  • foreach 引用了一个变量 OwnerWorkingInterests。这个变量在哪里定义?我在模型中没有看到它。
  • OwnerWorkingInterests 是存储 linq 查询结果的 ViewModel。
  • 你的编译器知道吗?您可能会遇到按需编译错误,这就是您看到通用异常的原因。
  • 老实说,我不知道我对 linq 很熟悉。
  • 好的,所以如果您的视图需要所有者模型,但您的查询没有返回该模型,那就是您的问题。稍后我会发布一个小例子。

标签: asp.net-mvc-5 asp.net-mvc-viewmodel


【解决方案1】:

如果视图需要一个 LandPortal.Models.Owner 类型的模型,并且 Index 返回整个 ActionResult,那么 Index 需要返回该类型的模型。

一个小例子:

public ActionResult Index(int Id)
{
    Landbase _db = new Landbase();
    Owner owner = new Owner();

    // some query has to set properties on this owner object
    // let's pretend there's a property named OwnerWorkingInterests on it

    owner.OwnerWorkingInterests = query.ToList();  // you will have to define "query" and set it similar to how you already did

    return View(owner);
}

现在您的视图可以访问模型上的属性了

@foreach (var owi in Model.OwnerWorkingInterests)

这是一个非常高级的示例,但我看到您有一个部分课程并在您的评论中提到了部分视图。如果您有一个大视图并试图将查询分解为多个部分,则可以使用 PartialViewResult 来完成,并且会与此有所不同。

【讨论】:

  • 所以基本上我必须将 ICollection 添加到我的所有者模型中?正确引用 WorkingInterest 视图模型
  • 是的,如果您还没有要使用的集合属性。相反,您可以使用部分视图结果并拥有一个只返回您的集合的方法,但这超出了这个特定问题的范围。
  • 另外,我不确定你是多么新,但为了清楚起见,这种方式会清除整个页面并替换它。您将失去之前模型上可能存在的任何其他属性。
  • 谢谢,我会试试看会发生什么
猜你喜欢
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
相关资源
最近更新 更多