使用列表在MVC 5视图中填充表的问题

时间:2018-08-02 17:03:46

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

我的控制器中包含以下代码:

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

1 个答案:

答案 0 :(得分:0)

如果视图需要一个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来完成,并且与此有所不同。