Skip to content

How to access different model properties in same view inMVC4

Sneha K

Asked Apr 3, 2016, 4:36 AM EDT3 answers
Share
LinkedInX
Hi Is it possible to access the different model properties for same view
 
My VisitorsViewModel model
 
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
 
 
My View_VisitorsForm Model 
 
C#
 public System.Guid VisitingID { get; set; }
    public string Employee { get; set; }
    public string CustomerName { get; set; }
    public Nullable<System.DateTime> VisitingDate { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public string SpendTime { get; set; }
    public string POVisit { get; set; }
 

I want to create 5 fields in view FromDate,Todate,CustomerName,Povisit ,StartTime,EndTimeEmployee. But all these fields properties in different models. Is it possible to access two different model properties in same view? i tried to explain my issue as per my level best. please any one give me solution.

Advance thanks..

 

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Saillesh Pawar
    0
    If you want these properties in the view it's better you add those properties in the viewmodel it self.  else you can access them by @using projectName.Model
    with this you will be able to access the class. It's better you create those properties in your view model class 
    0
  • 0
    Sailleshwar i want to keep fromdate and todate as textboxes and remaining fields are need to display the data which is fetch from Db same as like which is mention in the below image
                             
    so i have to access properties from different models. is it possible ? 
    0
  • Saillesh Pawar
    0
    You will face problems ,As i said you should create a viewmodel class for this view which contains all the properties which you need. because as you select the fromDate and toDate  it's should be binded to the controller model and once you return the records to the view than also the date will binded to the view.
     
    Create a ViewModel class

     public DateTime FromDate { get; set; }
    C#
    public DateTime ToDate { get; set; }
    public string ContactPerson  { get; set; }
    public string Description{ get; set; }
    C#
    public string Employee { get; set; }
    public string CustomerName { get; set; }
    public Nullable<System.DateTime> VisitingDate { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public string NextAppointment{ get; set; }
     
    and use this model 
    0