Daniel
Daniel

Reputation: 481

How do i add two models to one view?

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);
        }


Upvotes: 1

Views: 681

Answers (2)

Oluwadamilola Adegunwa
Oluwadamilola Adegunwa

Reputation: 359

use a ViewBag. i.e ViewBag.Ticket. So, in your controller, you can have

...
Ticket obj = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
ViewBag.Ticket = obj;
...

Then, in the view, you can have

Ticket obj = ViewBag.Ticket as Ticket;

Upvotes: 1

aliassce
aliassce

Reputation: 1197

Just create a new class to hold both Ticket and IEnumerable list.

public class TicketWithComments
{
public Ticket Ticket {get;set;}
public IEnumerable<Comment> Comments {get;set;}
}

Then in your controller assign the values from Db.

TicketWithComments t=new TicketWithComments();
t.Ticket=Ticket obj = _db.Tickets.FirstOrDefault(t => t.Ticket_Id == id);
 //I assume that there is a TicketId attribute in Comment class.
        t.Comments=_db.Comments.Where(f=>f.TicketId==id);
return View(t);

In your view you can use our new class now.

@model TicketWithComments

To reach comments of the ticket you can use foreach.

foreach (var comment in Model.Comments)
 {
  //show comment here.
 }

Alternatively, you can use Tuple in view as described here: https://stackoverflow.com/a/13823027/1431001

Upvotes: 3

Related Questions