Two data models in one view ( MVC)

I need to display text boxes of two classes in one view. I use the Entity framework database first approach and don’t know how to display 2 data models in one view. Also, I need to hide one of these class text boxes and show up on the click of a button.

Here is the first class "Task"

public partial class Tasks1 {     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]     public Tasks1()     {         this.Sub_tasks = new HashSet<Sub_tasks>();     }      public int Task_id { get; set; }     public string Name { get; set; }     public string Type { get; set; }     public Nullable<System.DateTime> Start_date { get; set; }     public Nullable<System.DateTime> End_date { get; set; }     public string Assigned_dept { get; set; }     public string Assigned_emp { get; set; }     public string Priority_type { get; set; }     public Nullable<bool> Status { get; set; }     public int Step_id { get; set; }      public virtual Step Step { get; set; }     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]     public virtual ICollection<Sub_tasks> Sub_tasks { get; set; } } 

This is the second

public partial class Step {     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]     public Step()     {         this.Tasks1 = new HashSet<Tasks1>();     }      public int Step_id { get; set; }     public string Step_type { get; set; }     public Nullable<bool> Status { get; set; }     public int Process_id { get; set; }      public virtual Process Process { get; set; }     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]     public virtual ICollection<Tasks1> Tasks1 { get; set; } } 

The error I got.

enter image description here

please help me! if my question is stupid, please be kind enough to explain this.

Add Comment
1 Answer(s)

You can pack these 2 models to 1 model, like this:

public class FullVM {     public Tasks1 MyTask {get;set;}     public Step MyStep {get;set;} } 

So your cshtml can be written like this:

@model YourNamespace.FullVM   @Html.IdFor(model => model.MyTask.Assigned_emp) @Html.EditorFor(model => model.MyStep.Process_id) 
Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.