Solved

Mvc Empty List linked to the Model

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to avoid redundand ajax calls 5 41
PHP Form Calculate Total Price 10 76
Firing an ASP.NET event on selection of a JQuery datepicker 4 30
ASP.NET MVC - Views 3 17
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to create an extensible mechanism for linked drop downs.
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 the basics of jQuery including how to code hide show and toggles. 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…

839 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