简体   繁体   English

如何在 mvc4 中的同一视图中访问不同的模型属性? 有可能吗?

[英]How to access different model properties in same view in mvc4?Is it possible?

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我的 View_VisitorsForm 模型

    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 , EndTime Employee .我想在视图FromDateTodateCustomerNamePovisitStartTimeEndTime Employee 中创建 5 个字段。 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..提前谢谢..

You should just create a new ViewModel, like:您应该只创建一个新的 ViewModel,例如:

public class CombinedVisitorsFormViewModel
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public string Employee { get; set; }
    public string CustomerName { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public string POVisit { get; set; } 
}

And wire (or map, or however you want to call it) every property in your Controller , the same way you do with current ViewModels.并连接(或映射,或者您想如何称呼它) Controller每个属性,就像您对当前 ViewModel 所做的一样。 ViewModel is a data representation for your particular view/views, so it can combine data from other models or even data not directly related with your DB Model at all. ViewModel 是您特定视图/视图的数据表示,因此它可以组合来自其他模型的数据,甚至与您的数据库Model根本不直接相关的数据。

As mentioned by mwilczynski, a view has one viewmodel only.正如 mwilczynski 所提到的,一个视图只有一个视图模型。 The idea being the model contains exactly the data the view needs to render the page.模型包含视图渲染页面所需的数据。 Ideally, nothing more and nothing less.理想情况下,不多也不少。

From your image, it looks as if you need a viewmodel with a collection of data items to render the grid (visiting date ... Next appointment).从您的图像来看,您似乎需要一个带有数据项集合的视图模型来呈现网格(访问日期...下一次约会)。 This is possible:这个有可能:

public class VisitorsViewModel
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }

    public ICollection<VisitViewModel> Visits { get; set; }
}

Where VisitViewModel is the viewmodel that contains the data retrieved from the database to show in the grid:其中VisitViewModel是包含从数据库中检索到的数据以显示在网格中的视图模型:

public class VisitViewModel
{
    public string CustomerName { get; set; }
    public string PoVisit { get; set; }
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    public string Employee { get; set; }
}

The controller would look something like:控制器看起来像:

public ActionResult displaycustomer()
{
    // Get data from database
    List<View_VisitorsForm> objvisitlist = (from v in db.View_VisitorsForm select v).ToList(); 

    // Create a new viewmodel and fill it with the data you want displayed
    VisitorsViewModel viewmodel = new VisitorsViewModel
    {
        FromDate = DateTime.Now.AddDays(-7), //(some default value)
        ToDate = DateTime.Now,               //(some default value)
        Visits = new List<VisitViewModel>()
    };

    foreach (View_VisitorsForm visit in objvisitlist)
    {
        viewmodel.Visits.Add(new VisitViewModel
        {
            CustomerName = visit.CustomerName,
            PoVisit = visit.POVisit,
            StartTime = visit.StartTime,
            EndTime  = visit.EndTime,
            Employee = visit.Employee
        }
    }

    // return a ViewResult with the viewmodel 
    return View(viewmodel);
} 

In the view, you can access the properties in the ICollection<VisitViewModel> like so (I assume you want the visit date in a table):在视图中,您可以像这样访问ICollection<VisitViewModel>的属性(我假设您想要表中的访问日期):

<table>
@foreach (var visit in Model.Visits)
{
    <tr>
        <td>
            @Html.DisplayValueFor(m => visit.CustomerName)
        </td>
        <td>
            @Html.DisplayValueFor(m => visit.PoVisit)
        </td>
    </tr>
}
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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