【发布时间】: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