Solved

Mvc Empty List linked to the Model

Posted on 2010-09-24
2
773 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
There is a huge demand for CodeIgniter among the PHP web developers due to its dynamic features and benefits these days. It is one of most popular and agile open source PHP framework for creating robust web applications in PHP web development field.…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now