简体   繁体   中英

How to display data from one View to another ASP.NET CORE 5.0 MVC

If I have for example model Ticket.cs which contain following field

public class Ticket
    {
        public int Id { get; set; }

        [Display(Name = "Opis")]
        public string Description { get; set; }

        [Display(Name = "Datum i vrijeme")]
        public DateTime DateAndTime { get; set; } = DateTime.Now;

        [Display(Name = "Vrsta tiketa")]
        public int TicketTypeID { get; set; }

        [ForeignKey("TicketTypeID")]
        public virtual TicketType TicketType { get; set; }

        public int? ClientId { get; set; }
        [ForeignKey("ClientId")]
        public Client Client { get; set; }
    }

And in Index.cshtml I display this data. Another Model Discussion.cs

public class Discussion
    {
        [Key]
        public int Id { get; set; }        

        [Display(Name = "Odgovor")]
        public string Answer { get; set; }
      
        public string UserId { get; set; }

        [ForeignKey("UserId")]
        public virtual ApplicationUser ApplicationUser { get; set; }

        public bool IsActive { get; set; } = true;

    }

Right now, I want data from Ticket.cs transfer to Discussion Index.cshtml to be display like

DateTime , Description

What Do I need to do in my controller to get this kind of data exactly same as it is in Ticket model Please let me know if you need more information or source code. I didn't put much code in order to post be more clear. Is the best way to pass to my Discussion Model

public Ticket ticket {get;set;} 

Or create ViewModel

UPDATE

Once I include in my View

@model VmSTicketing.Models.ViewModels.DiscussionTicketVM

And when I use foreach look

@foreach (var item in Model)
                {
                    <tr>
                        <td>@item</td>
                        <td>08/14/2017</td>
                        <td>nn</td>
                        <td>nn</td>
                        <td>Website problem</td>
                        <td><span class="text-primary">Open</span></td>
                    </tr>
                }

I get error message

foreach statement cannot operate on variables of type 'DiscussionTicketVM' because 'DiscussionTicketVM' does not contain a public instance or extension definition for 'GetEnumerator'

In such cases, you should use the ViewModel . The ViewModel is a combination of several models that are displayed in one view.

Suppose you have a view in which both customer information and product information must be displayed. To do this, you need to create a class and create an instance of each model that appears in the view.

public class DiscussionTicketViewModel
{
    public Discussion discussion { get; set; }
    public Ticket ticket { get; set; }
}

In controller

public ActionResult Index()
{
    var model = new DiscussionTicketViewModel();
    model.ticket = //.............
    model.discussion = //.........
    return View(model);
}

In View

<tr>
  <td>@Model.ticket.Description</td>
  <td>08/14/2017</td>
  <td>nn</td>
  <td>nn</td>
  <td>Website problem</td>
  <td><span class="text-primary">Open</span></td>
</tr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM