简体   繁体   English

嵌套对象的模型绑定

[英]Model binding for nested objects

I have a class 我上课了

 public class Offer
{
    public Int32 OfferId { get; set; }
    public string OfferTitle { get; set; }
    public string OfferDescription { get; set; }

}

and another class 和另一堂课

 public class OfferLocationViewModel
{
    public Offer Offer { get; set; }
    public Int32 InTotalBranch { get; set; }
    public Int32 BusinessTotalLocation { get; set; }
}

Now in my controller I have the following 现在在我的控制器中我有以下内容

 public ActionResult PresentOffers(Guid id)
    {
        DateTime todaysDate=Utility.getCurrentDateTime();

        var rOffers=(from k in dc.GetPresentOffers(id,todaysDate)
                     select new OfferLocationViewModel()
                     {
                        Offer.  //I dont get anything here..

                     }).ToList();


        return PartialView();
    }

Now the problem is in my controller, I can not access any property of the 'Offer' class !! 现在问题在于我的控制器,我无法访问'Offer'类的任何属性! I thought, since i am creating a new OfferLocationViewModel() and this has a property of type 'Offer', I will be able to access the properties..But I can not. 我想,因为我正在创建一个新的OfferLocationViewModel(),并且它有一个'Offer'类型的属性,我将能够访问属性..但我不能。

Can anyone give me some idea about how to do that? 任何人都可以告诉我如何做到这一点?

In a class initializer like new OfferLocationViewModel { ... } you can only set the immediate properties, ie 'Offer = new Offer()'. 在像new OfferLocationViewModel { ... }这样的类初始值设定项中,您只能设置直接属性,即'Offer = new Offer()'。

You can't access the contained type's properties through the initializer. 您无法通过初始化程序访问包含类型的属性。

Though you can initialize the view model's Offer to a new Offer with the given properties like this: 虽然你可以初始化视图模型的Offer与这样的性能给出了一个新的报价:

var rOffers = (from k in dc.GetPresentOffers(id,todaysDate)
               select new OfferLocationViewModel {
                   Offer = new Offer {
                       OfferId = ...,
                       OfferTitle = ...,
                       OfferDescription = ...
                   }
               }).ToList();

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

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