How should I handle lookups in my ViewModel?

My database table for buildings stores the building type as a code. In a separate lookup table the description for that code is stored.

How should I design my ViewModel and where will I need to make the call to get the associated description value?

I sort of can see one option. I want to know if there is a better option.

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

Then in my view

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

Am I incorrect in the way I am thinking? if I do the above I create a dependency in my View to the service?

Update 1

Working through some of the solutions offered. I seem to run into another issue. I can't access the constructor of each building directly...

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; } }

--------------Solutions-------------

My understanding is that the view model is meant for display ready data. I think the real problem here is putting model dependent logic into the view.

You can do your service lookup but keep that code in the controller. The view model should be considered display ready (save for some formatting).

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

and then do the lookup before you render:

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

Having come from a long line of JSP custom tags I dread having any code hidden in the view layout. IMO, that layer should be a dumb as possible.

I would recommend having a helper that does that, or a DisplayTemplate

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

or

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

More info on templates: http://www.headcrash.us/blog/2011/09/custom-display-and-editor-templates-with-asp-net-mvc-3-razor/

Both of these approaches introduce a dependency on your service but you can test/change them in one single place, instead of the whole application (plus the usage looks cleaner).

Category:c# Time:2012-02-21 Views:0

Related post

  • handling lookup tables with deleted records and databound controls 2009-01-15

    I have a table Resource with a field Type. Type is a lookup into the table ResourceType. So for instance ResourceType might have: 1: Books 2: Candy 3: Both And Resource might have 1: Tom's Grocery, 2 2: Freds News, 3 It would display as: Tom's Grocer

  • Handling fatal exceptions in ViewModel/Model 2010-03-01

    I have an application written using the M-V-VM approach. The data access is done in the Model. If a fatal error occurs here (for example, the connection to the data source is lost), and Exception is thrown. This Exception bubbles up to the ViewModel.

  • Django - how to handle lookup that return Zero rows 2011-04-23

    What is the best way to deal with 0 records return from a database lookup? I have UserDetails object (table) that has a foreign key to the default django User object (table) How to deal with Users who don't yet have any row/records in the UserDetails

  • How to handle cleanup of my viewmodels when i remove the view from call stack 2012-03-21

    In my application I am removing some views from my backstack entry in my Windows phone application. My issue is that while removing the views the viewmodel corresponding to that view are not destroying. I had written the cleanup in by backkeypress ev

  • Handling lookup tables in Entity Framework v1 2009-05-28

    I am just getting started with Microsoft's Entity Framework, using it for an MVC project since MS seems to be really pushing it, and I'm running into some issues. In my database there are multiple lookup tables tied to a single table via foreign keys

  • MVVM: How to handle interaction between nested ViewModels? 2010-04-05

    I'm been experimenting with the oft-mentioned MVVM pattern and I've been having a hard time defining clear boundaries in some cases. In my application, I have a dialog that allows me to create a Connection to a Controller. There is a ViewModel class

  • c# handling lookup values from database 2011-03-09

    When you have a lookup table in a sql database. If you wanted to use the value in an expression would you use the primary key (integer) or the actual value of the record. Sorry if thats a bit vague. Ill show you an example to explain. sql table id |

  • How to handle LOOKUP if "looked-up" term is not present in the "searched" array? 2013-10-24

    I want to search a list of 'exhibitors' (maybe 1000 names) and add to it the name of our contact, if we already have a contact for that exhibitor in our 'contacts' list of perhaps 100 names. In the 'exhibitors' worksheet I am using LOOKUP to search t

  • How to handle wpf datagrid MouseMove event in viewmodel-in MVVM pattern? 2010-07-02

    HI, I have a wpf-datagrid in view, i am following mvvm model...so no code in "codebehind". I wud like to handle "mousemove" event in viewmodel..how do i do this ? --------------Solutions------------- I would handle this in the code behind! The MVVM-p

  • What's quicker, an array lookup (including array build) or an IF stack? 2008-12-05

    I was wondering which was better: $lookup = array( "a" => 1, "b" => 2, "c" => 3 ); return $lookup[$key]; or if ( $key == "a" ) return 1 else if ( $key == "b" ) return 2 else if ( $key == "c" ) return 3 or maybe just a nice switch... switch($

  • Handling Dialogs in WPF with MVVM 2009-01-18

    In the MVVM pattern for WPF, handling dialogs is one of the more complex operations. As your view model does not know anything about the view, dialog communication can be interesting. I can expose an ICommand that when the view invokes it, a dialog c

  • ViewModel/View relationship and validation 2009-04-07

    In our WPF application we want to use the basic MVVM pattern. We were discussing it and some uncertainties about ViewModel/View relationship and validation came up. Would you say the following is a good understanding of it? Every View has one and onl

  • WPF: How to attach mouse events to a viewmodel? 2009-05-15

    I am trying to use the MVVM pattern for the first time. So I have an ItemsControl filled with my viewmodel objects, displayed using DataTemplate's; the objects are "nodes" and "edges" represented in DataTemplate's with Thumb and Polyline objects and

  • Simple Event Handling in MVVM 2009-12-18

    Just wondering what people had for ideas on how best to handle events in a ViewModel from controls on a View ... in the most lightweight way possible. Example: <MediaElement MediaOpened={Binding SomeEventHandler} /> In this case we want to hand

  • MVVM Clarification: What belongs into ViewModel, what belongs into Model? 2010-02-17

    So I was looking into MVVM again after some time where I didn't really follow new developments and I noticed that the amount of tutorials/guides etc. has grown greatly. However most examples/example implementations of MVVM lack to explain something t

  • ViewModel with SelectList binding in ASP.NET MVC2 2010-02-25

    I am trying to implement an Edit ViewModel for my Linq2SQL entity called Product. It has a foreign key linked to a list of brands. Currently I am populating the brand list via ViewData and using DropDownListFor, thus: <div class="editor-field">

  • Should a ViewModel in MVVM reference the View? 2010-09-08

    In the MVVM (Model-View-ViewModel) pattern should the ViewModel reference the view. I would think that it should not. But how should the following scenario be handeled? I have a view that has a tab control as the main container, the viewmodel for thi

  • Handling the window closing event with WPF / MVVM Light Toolkit 2010-09-10

    I'd like to handle the "Closing" event (when a user clicks the upper right 'X' button) of my window in order to eventually display a confirm message or/and cancel the closing. I know how to do this in the code-behind : subscribe to the "Closing" even

  • WPF and MVVM: Enable a command only when a textbox has focus with implementation in ViewModel? 2010-12-30

    I have a command that I would like to be enabled only when a certain control has focus. I can do this with a routed command and command binding, but I'd like to keep the implementation in my ViewModel. Is a command binding and an event handler in the

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.617 (s). 13 q(s)