Solved

Mvc Empty List linked to the Model

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.…
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 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…

739 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