Reputation: 427
EDIT - I have made progress from the original post and have now made the relevant changes to the question.
In the controller I now have a variable called lstSteps that brings in all the details of the steps that are associated with the relevant Execution.
The problem now is that I am unable to show this list of Steps in the Execution view. I understand that I need to write a foreach loop but cant figure out how to write it.
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>
I believe the parts I am going wrong on is where I have placed the ?
Help would be much appreciated.
Thanks in advance.
Upvotes: 1
Views: 2276
Reputation: 5600
So add navigation property to Step
class as you have a foreign key to Execution
class :
public virtual Execution Execution {get; set; }
And your Linq
query would look like this:
var lstSteps = db.Steps.Where(z => z.Execution.Id == id).ToList();
This will get you all Steps
where the execution id is equal to the id you are passing. You can Iterate though lstSteps and print it in your view. If you know it will have only one result then use .FirstOrDefault()
instead of ToList()
EDIT :
Ok so you want to show a list of Executions with their corresponding Step.
Add another navigation property in Execution
:
public virtual ICollection<Step> Steps {get; set; }
change your linq to (this should get steps
too):
var Executions = db.Executions.Find(id);
In your view you can do a foreach :
@foreach(var step in Model.Steps)
{
<label>Step id</label>step .Id;
<label>step Name </label>step.Name
....
}
Make sure you your model is : @model DataIntelligence.Models.Execution
Upvotes: 1
Reputation: 8960
Just define a new class like this and pass it to your view as your model
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;
}
}
}
You can then load ExecutionStep call and can pass it to your view like this
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);
}
You can access them in your view like this
@Modal.Execution.Start
@foreach(var step in Modal.Steps)
{
}
Upvotes: 0