Sunday, November 4, 2018

A1 Repro (ASP.Net CORE): Loading Lookup Collection in Action Filter and Simple Dropdownlist Editor

There are times when a collection of lookup to populate a drop down list is needed; this is very common. The usual solution is to create an Enumerable property in the view model to hold the lookup values, inject a service in the controller then in the action load the list to the view model and load the view. A complete example can be accessed at Pluralsight where dropdown lists are polulated in Razor views using the MVVM design pattern, Entity Framework, and Ajax.

Another approach is to load the lookup collection to the ViewData and access the values from the view or a template editor. This note is about a modification of this approach where the lookup collection is loaded into the ViewData using a Fiter Service which decorates the action requiring the loopkup collection.

The Filter
The following filter loads a collection of AssetTypes and converts the collection of DTOs into a SelectListItems using an extension method and loads the list on the ViewData.



The way to consume this filter service attribute is through the ServiceFilter, decorating the action with [ServiceFilter(typeof(AssetTypeCollectionAttribute))] this way:



Inject Service
Like any other service, the Filter Service needs to be injected in order to be discovered, therefore for an ASP.Net CORE application AssetTypeCollectionAttribute needs to  be injected.  In this example is added for the scope of the request:



Simple Drop Down List Editor
The editor template is simple, and can be reused for other lookup lists.



ViewModel and View
To end the note; the editor is invoked from the view passing two parameters in the ViewData; the index or key used to store the collection in the ViewData and the initial default message to load in the drop down list.



None of the above would work if the ViewModel does not have property AssetType decorated with [UIHint("ViewDropdown")]





To Table of contents

12 comments:

  1. Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post. price monitoring

    ReplyDelete
  2. Understanding the advantages of the SaaS model may require crucial changes to a software merchant's plan of action, software engineering and operational structure. Rainy Crack

    ReplyDelete
  3. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. 50 Cent net worth

    ReplyDelete
  4. That appears to be excellent however i am still not too sure that I like it. At any rate will look far more into it and decide personally! accounting software Malaysia

    ReplyDelete
  5. Generate your own targeted business sales leads with laser precision on complete auto pilot with revolutionary CBT Web Scraper and Email Extractor Software. Scrape and extract business data from Google Maps, Google SERPs, Yellow Pages, Yelp, Facebook Business Pages, Twitter, LinkedIn and custom website lists. Download the only lead generation tool you will ever need right now. Software

    ReplyDelete
  6. I curious more interest in some of them hope you will give more information on this topics in your next articles. monero online wallet

    ReplyDelete
  7. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. Email Address Extractor Online

    ReplyDelete
  8. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks How is web scraping used in business?

    ReplyDelete

A1 Repo: Simple Pagination for WCF Service Operation

It is never a good idea to paginate on the client.  This post is about a simple pagination for a WCF service operation.  For this work I nee...