I have two models, a ticket and one called comment. I have a one to many relationship between ticket to comments. What I want to do is be able to use data from both my models for one view. I should be able to show data from both tickets and comments.
As you can see, I have put my @model.Models.Ticket in the view to Ticket to get data from ticket to the view.
@model WebApplication20.Models.Ticket
@{
Layout = "_Dashboard";
var title = "About Ticket";
}
<html>
<body id="page-top">
<div class="card mx-auto" style="width: 18rem;">
<div class="card-header">
<h4><strong>Ticket Status</strong></h4> Created @Model.TicketCreated
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item"><strong>Name:</strong> </li>
<li class="list-group-item"><strong>Descripton:</strong></li>
<li class="list-group-item"><strong>Priority:</strong> @Model.TicketPriority</li>
<li class="list-group-item"><strong>Type:</strong> @Model.TicketType</li>
<li class="list-group-item"><strong>Status:</strong> @Model.TicketStatus</li>
</ul>
</div>
<div class="card shadow mx-auto m-3" style="width: 18rem;">
<div class="card-header">
<h4><strong>Comments</strong></h4>
</div>
@*<div class="form-group row">
<div class="col-4">
<label asp-for=""></label>
</div>
<div class="col-8">
<textarea asp-for="" rows="5" style="resize:none;" class="form-control"></textarea>
<span asp-validation-for="" class="text-danger"></span>
</div>
</div>*@
</div>
I have also tested making a view model.
public class CommentVM
{
public Ticket Ticket { get; set; }
public Ticket Ticket_Id { get; set; }
public IEnumerable<SelectListItem> ProjectList { get; set; }
public IEnumerable<SelectListItem> StatusList { get; set; }
public string TicketStatus { get; set; }
public DateTime TicketCreated { get; set; }
public string TicketPriority { get; set; }
public string TicketType { get; set; }
// Comment
public Comments Comments { get; set; }
public Comments Comment_Id { get; set; }
public string Message { get; set; }
}
But the problem is that I need a unique ticket to be able to see the right values. And therefore I use the following code in my controller. From what I have tested, I can not get a unique value to my view by adding a view model to my controls?
This is the controlleri want my view to show data from both ticket and comment.
public IActionResult Info(int id)
{
if (id == null)
{
return NotFound();
}
Ticket obj = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
return View(obj);
}