我应该如何处理ViewModel中的查找?

时间:2012-02-21 15:48:51

标签: c# asp.net-mvc-3 razor decoupling

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

我应该如何设计我的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; }

    }

2 个答案:

答案 0 :(得分:3)

我的理解是视图模型用于显示就绪数据。我认为这里真正的问题是将模型相关逻辑放入视图中。

您可以进行服务查找,但将该代码保存在控制器中。应将视图模型视为显示就绪(除了某些格式化)。

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

然后在渲染之前执行查找

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

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

答案 1 :(得分: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/

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