2
Reply

How to access different model in same view in mvc4?

Sneha Dhandapani

Sneha Dhandapani

Apr 4 2016 2:14 AM
367
                              
 
This is my view . if i open this view it want to load  all the list  of  data which is mention in the below  image   eg  
  
                        But the properties of   FromDate ,ToDate and VisitingDate,CustomerName,EmployeemPurpose of visit ,ContactPerson Email are in different models.
 
My VisitorsViewModel model
 
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
 
My View_VisitorsForm Model
 
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; }
 
 
so i decided to access two model properties in same view so i call one model property in another model whixch is mentioned in the below model
 
 
My VisitorsViewModel model
 
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
 
public ICollection<View_VisitorsForm> Visits { get; set; } 
 
 
My controller code
 
 
public ActionResult displaycustomer()
{
List<View_VisitorsForm> objvisitlist = (from v in db.View_VisitorsForm select v).ToList();
return View(objvisitlist);
}
 
 
This is my  Controller code  
 
List<View_VisitorsForm> objvisitlist = (from v in db.View_VisitorsForm select v).ToList();
 
this is the query to list all the data. 
 
 
My view
 
@model Sample_Customer.View_VisitorsForm
@*@model IEnumerable<Sample_Customer.>*@
@{
ViewBag.Title = "DailyVisitReport";
}

<div class="text-center">

<h2 style="color: #0000FF">Visit Report</h2>
</div>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Content/BootStrap/js/bootstrap.js"></script>
<script src="~/Content/BootStrap/js/bootstrap.min.js"></script>

<div class="col-xs-12">
<div class="container">
<div class="col-sm-4">
<div class="form-group">
@Html.LabelFor(model=>model.FromDate)
@Html.TextBoxFor(model=>model.FromDate,new { @class = "form-control", type = "text" })

</div>
</div>
<div class="col-sm-4">
<div class="form-group">

@Html.LabelFor(model => model.ToDate)
@Html.TextBoxFor(model => model.ToDate, new { @class = "form-control", type = "text"})

</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<input id="Button1" type="button" value="Ok" />
</div>
</div>
</div>
</div>

<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="color: #1a1ae9">Visiting Date</th>
<th style="color: #1a1ae9">Customer Name</th>
<th style="color: #1a1ae9">Employee</th>
<th style="color: #1a1ae9">Purpose of Visit</th>
<th style="color: #1a1ae9">Contact Person</th>
<th style="color: #1a1ae9">Description</th>
<th style="color: #1a1ae9">Next Appointment</th>
</tr>
</thead>

<tbody>
@foreach (var i in Model.??)( in this place i donno which object i have to call here)
{
<tr>
<td>
@Convert.ToString(string.Format("{0:MMM-dd-yyyy}", i.VisitingDate))
</td>
<td>
@i.CustomerName
</td>
<td>
@i.Employee
</td>
<td>
@i.POVisit
</td>
<td>
@i.ContactPerson
</td>
<td>
@i.Description
</td>
<td>
@Convert.ToString(string.Format("{0:MMM-dd-yyyy}", i.NextAppointment))
</td>
</tr>
}
</tbody>
</table>

 
My doubt is  i call the one model (view_visitorsForm)  in VisitorsViewmodel by
 
public ICollection<View_VisitorsForm> Visits { get; set; }
 
and i have to list all the data in view when i open the page .so  in controller i wrote this code
 
List<View_VisitorsForm> objvisitlist = (from v in db.View_VisitorsForm select v).ToList();
 
Now i created the view by giving model name as VisitorsViewModel. Now my doubt is  which object  i have to call in the place of ??. so it will display all the data when i open the page..
 
 
i tried to explain my issue as  per my level best please any one understand my issue and help me to solve this problem.
 
 
Advance thanks.
 
 
 
 
 
 
 
 
 

Answers (2)