编辑 - 我从原帖发了进展,现在对问题进行了相关修改。
在控制器中,我现在有一个名为lstSteps的变量,它带来了与相关执行相关的步骤的所有细节。
现在的问题是我无法在执行视图中显示此步骤列表。我知道我需要编写一个foreach循环,但无法弄清楚如何编写它。
public ActionResult Execution(int id = 0)
{
Execution execution = db.Executions.Find(id);
var lstSteps = db.Steps.Where(z => z.Execution.Id == id).ToList();
if (execution == null)
{
return HttpNotFound();
}
ViewBag.ExecutionSeconds = (execution.End - execution.Start).ToString(@"dd\.hh\:mm\:ss\.fff");
return View(execution);
}
namespace DataIntelligence.Models
{
[Table("Execution")]
public class Execution
{
public int Id { get; set; }
public Int16 PackageId { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public Boolean Successful { get; set; }
}
}
namespace DataIntelligence.Models
{
[Table("Step")]
public class Step
{
public int Id { get; set; }
public int ExecutionId { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public virtual Execution Execution { get; set; }
}
}
@model DataIntelligence.Models.Execution
@{
ViewBag.Title = "Execution Details";
}
<script language="javascript" type="text/javascript" src="jquery-1.8.3.js"> </script>
<script>
function goBack() {
window.history.back();
}
</script>
<div class="jumbotron">
<h2>Execution Details</h2>
</div>
<fieldset>
<legend>Execution Id - @Html.DisplayFor(model => model.Id)</legend>
<div class="row">
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.PackageId)
</div>
<div class="display-field">
<a href="@Url.Action("Package", new { id=Model.PackageId})"> @Html.DisplayFor(model => model.PackageId) </a>
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.Start)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Start)
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.End)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.End)
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
Execution Time
</div>
<div class="display-field">
@ViewBag.ExecutionSeconds
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.Successful)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Successful)
</div>
</div>
</div>
<br />
</fieldset>
<table>
<tr>
<th>
Step ID
</th>
<th>
Name
</th>
<th>
Date
</th>
</tr>
@foreach (var ? in ?) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td style="width: 600px;">
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
</tr>
}
</table>
<button class="btn btn-link" onclick="goBack()" style="padding-right: 0px; padding-top: 0px; padding-bottom: 0px; padding-left: 0px;">Back</button>
我相信我出错的部分是我放置的地方?
非常感谢帮助。
提前致谢。
答案 0 :(得分:1)
因此,如果您拥有Step
类的外键,请将导航属性添加到Execution
类:
public virtual Execution Execution {get; set; }
您的Linq
查询将如下所示:
var lstSteps = db.Steps.Where(z => z.Execution.Id == id).ToList();
这将为您提供所有Steps
,其中执行ID等于您传递的ID。您可以通过lstSteps迭代并在视图中打印它。如果您知道它只有一个结果,那么请使用.FirstOrDefault()
代替ToList()
编辑:
好的,所以你想要显示一个执行列表及其相应的步骤。
在Execution
中添加其他导航属性:
public virtual ICollection<Step> Steps {get; set; }
将你的linq更改为(这也应该得到steps
):
var Executions = db.Executions.Find(id);
在你看来,你可以做一个foreach:
@foreach(var step in Model.Steps)
{
<label>Step id</label>step .Id;
<label>step Name </label>step.Name
....
}
确保您的模型是:@model DataIntelligence.Models.Execution
答案 1 :(得分:0)
只需定义一个这样的新类,并将其作为模型传递给您的视图
namespace DataIntelligence.Models
{
public class ExecutionStep
{
public Execution Execution { get;set; }
public List<Step> Steps { get;set; }
public static ExecutionStep Load(int executionStepId)
{
// load your execution and step data here and return it
// within ExecutionStep class object
ExecutionStep executionStep = new ExecutionStep();
executionStep.Execution = db.Executions.Find(id);
executionStep.Steps = db..... // load steps from db
return executionStep;
}
}
}
然后您可以加载ExecutionStep调用并将其传递给您的视图
public ActionResult Execution(int id = 0)
{
// now this executionStep class object will serve as a modal
// for your view
ExecutionStep executionStep = ExecutionStep.Load(id);
if (execution == null)
{
return HttpNotFound();
}
ViewBag.ExecutionSeconds = (execution.End - execution.Start).ToString(@"dd\.hh\:mm\:ss\.fff");
return View(executionStep);
}
您可以在视图中访问它们
@Modal.Execution.Start
@foreach(var step in Modal.Steps)
{
}