我遇到了一个我以前没有遇到的疯狂问题。我在视图中有一些剃须刀从数据库列生成标签和单选按钮。
以下是我在视图中循环的示例:
@foreach(var item in Model)
{
foreach (var cat in item.PainCategories)
{
<h3><b>@Html.Label(cat.Title, new { cats = cat.Title.Distinct() })</b></h3>
<div align="left" style="font-size:small">
@foreach(var tri in item.Triages)
{
if (cat.Id == tri.PainCategoryId)
{
@Html.RadioButton(tri.PainCategory.Title, tri.Order)
@Html.Label(tri.Description )<br/>
//Error is happening in the line above
}
}
</div>
}
}
这是我的数据库表的一个例子:
Id Order Description
1 1 Desc 1
2 2 Desc 2
3 3 Desc 3
4 4 Desc 4
5 5 Desc 5
6 6 Desc 6
所以我的问题是它会填充内部foreach循环中单选按钮的所有try.Order。但是当它到达tri.Description时。它只打印前4个描述,其余的不显示。我尝试过使用@ Html.Display,同样的事情发生了。我知道它正在通过每个foreach,因为订单将使它达到6但描述停在4。奇怪的问题,如果有人可以给我一些建议,将不胜感激。如果您需要查看更多代码(例如控制器或模型),请告诉我。谢谢。
---- ----编辑
因此控制器返回一个JSON对象列表,这些对象存储在DefectsVM模型中并显示在视图中的数据表中。
控制器:
var requestDef = new RestRequest("defect", Method.GET);
requestDef.AddHeader("id", id);
requestDef.AddHeader("key", Key);
requestDef.RequestFormat = DataFormat.Json;
var responseDef = Userclient.Execute(requestDef) as RestResponse;
List<DefectsVM> defects = JsonConvert.DeserializeObject<List<DefectsVM>>(responseDef.Content);
IEnumerable<DefectsVM> defectsToReturn = defects.Where(d => d.ProjectId == item.ProjectId);
foreach(var i in defectsToReturn)
{
i.PainCategories = db.PainCategories.Where(p => p.CompanyId == compId).Where(t => t.CompanyId == compId).ToList().Distinct();
i.Triages = db.Triages.ToList().Where(t => i.PainCategories.Any(cat => t.PainCategoryId == cat.Id));
}
return View(defectsToReturn);
缺陷VM模型:
public class DefectsVM
{
public int Id { get; set; }
public string Title { get; set; }
public double UserPainIndex { get; set; }
public Nullable<int> AssignedToId { get; set; }
public int ProjectId { get; set; }
public Nullable<int> ReportedById { get; set; }
public IEnumerable<PainCategory> PainCategories { get; set; }
public IEnumerable<Triage> Triages {get; set;}
}