Solved

Mvc Empty List linked to the Model

Posted on 2010-09-24
2
780 Views
Last Modified: 2013-11-08
In combination with a ViewModel (for my business logic) I am using the ViewData[""] object to pass lists to my view with data from the database.

I am using 3 lists and I link them to my Model
Only one of these lists has items fetched from the database like so:

ViewData["Materials"] = StockRepository.GetMaterialTypes();

Open in new window


However... the other 2 lists should be empty by default and will get filled using jQuery when the user selects an item from the first list.

Right now, I have to fake 2 empty lists by returning an empty search from the database.

ViewData["Brands"] = StockRepository.GetBrands();
ViewData["ModelTypes"] = StockRepository.GetModelTypes();

Open in new window


The GetBrands and GetModelTypes methods actually return nothing. They do something like this to make sure nothing is returned!

        public static IEnumerable<SelectListItem> GetBrands()
        {
            StockManagementDataContext db = new StockManagementDataContext();

            var brands = from m in db.Material
                         [b]where m.Description == "ABC"[/b]
                         select new SelectListItem
                         {
                             Text = m.Description,
                             Value = m.MaterialId.ToString(),
                         };

            return brands;
        }

Open in new window


My question: is it possible to initialize ViewData["Brands"] with an empty list or is there another more efficient way to link the model to an empty list by default?

Thx

Gerrie
0
Comment
Question by:gerrie-govaerts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
ID: 33754568
Do I understand you correctly. You simply want to return an empty list/enumerator? Then try this (Got this from http://stackoverflow.com/questions/1714351/return-an-empty-ienumerator) (I am assuming you are using C# from the IEnumerable):
public static IEnumerable<SelectListItem> GetBrands()
        {
            return Enumerable.Empty<SelectListItem>();
        }

Open in new window

0
 

Author Closing Comment

by:gerrie-govaerts
ID: 33758283
Thx that's working - I don't like to use it but I needed some way to link the selected item from a listbox to the ViewModel - but I'll figure it out :-)
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question