1

我的建筑物数据库表将建筑物类型存储为代码。在单独的查找表中存储了该代码的描述。

我应该如何设计我的ViewModel,我需要在哪里进行调用以获取相关的描述值?

我可以看到一个选项。我想知道是否有更好的选择。

BuildingViewModel
{
 public string BuildingTypeCode { get;set;}  
 ...other properties
}

那么在我看来

code...

<p>@MyService.GetDescription(Model.BuildingTypeCode)</p>

...code

我的思维方式不正确吗?如果我执行上述操作,我会在我View的服务中创建一个依赖项?

更新 1

研究提供的一些解决方案。我似乎遇到了另一个问题。我无法直接访问每个建筑物的构造函数...

    public ViewResult Show(string ParcelId)
    {
        var result = _service.GetProperty(ParcelId);
        var AltOwners = _service.GetAltOwners(ParcelId);
        var Buildings = _service.GetBuildings(ParcelId);

        ParcelDetailViewModel ViewModel = new ParcelDetailViewModel();
        ViewModel.AltOwnership = new List<OwnerShipViewModel>();
        ViewModel.Buildings = new List<BuildingViewModel>();

        AutoMapper.Mapper.Map(result, ViewModel);
        AutoMapper.Mapper.Map<IEnumerable<AltOwnership>, IEnumerable<OwnerShipViewModel>>(AltOwners,ViewModel.AltOwnership);
        AutoMapper.Mapper.Map<IEnumerable<Building>, IEnumerable<BuildingViewModel>>(Buildings, ViewModel.Buildings);
        ViewModel.Pool = _service.HasPool(ParcelId);
        ViewModel.Homestead = _service.IsHomestead(ParcelId);

        return View(ViewModel);
    }

public class ParcelDetailViewModel
    {
        public IEnumerable<OwnerShipViewModel> AltOwnership { get; set; }
        //public IEnumerable<ValueViewModel> Values { get; set; }
        public IEnumerable<BuildingViewModel> Buildings { get; set; }
        //public IEnumerable<TransferViewModel> Transfers { get; set; }
        //public IEnumerable<SiteAddressViewModel> SiteAddresses { get; set; }

        public string ParcelID { get; set; }
        //public string ParcelDescription { get; set; }
        //public int LandArea { get; set; }
        //public string Incorporation { get; set; }
        //public string SubdivisionCode {get;set;}
        public string UseCode { get; set; }
        //public string SecTwpRge { get; set; }
        //public string Census { get; set; }
        //public string Zoning { get; set; }
        public Boolean  Homestead {get;set;}

        //public int TotalBuildingArea { get; set; }
        //public int TotalLivingArea { get; set; }
        //public int LivingUnits { get; set; }
        //public int Beds { get; set; }
        //public decimal Baths { get; set; }
        public short Pool { get; set; }
        //public int YearBuilt { get; set; }
        
    }
4

2 回答 2

4

我的理解是视图模型是为了显示准备好的数据。我认为这里真正的问题是将模型相关逻辑放入视图中。

您可以进行服务查找,但将该代码保留在控制器中。应将视图模型视为已准备好显示(保存某些格式)。

class BuildingViewModel 
{ 
    public string BuildingTypeCode { get;set;}   
    ...other properties 
} 

然后在渲染之前进行查找:

public ActionResult Building()
{
    var typeCode = // get from original source?
    var model = new BuildingViewModel
    {
        BuildingTypeCode = MyService.GetDescription(typeCode)
    };
    return View("Building", model);
}

来自一长串 JSP 自定义标签,我害怕在视图布局中隐藏任何代码。IMO,该层应尽可能愚蠢。

于 2012-02-21T16:32:38.130 回答
0

我会建议有一个帮手来做这件事,或者一个 DisplayTemplate

public class ViewHelpers
{
  public static string GetDescription(string code)
  {
    MyService.GetDescription(Model.BuildingTypeCode);
  }
}

或者

@ModelType string
@Html.DisplayFor("",MyService.GetDescription(Model.BuildingTypeCode));

有关模板的更多信息:http ://www.headcrash.us/blog/2011/09/custom-display-and-editor-templates-with-asp-net-mvc-3-razor/

这两种方法都引入了对您的服务的依赖,但您可以在一个地方测试/更改它们,而不是整个应用程序(加上使用看起来更干净)。

于 2012-02-21T15:51:48.193 回答