Solved

Mvc Empty List linked to the Model

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
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)

757 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

18 Experts available now in Live!

Get 1:1 Help Now