Solved

Default value is not being set in my SelectList in MVC4 View

Posted on 2014-04-04
3
392 Views
Last Modified: 2014-04-14
I have a View that displays a dropdown using the following in my view:

@Html.DropDownListFor(model => model.databaseServerTypeID, (SelectList)@ViewBag.databaseServerTypeID, "-- Choose Server Type --")

Open in new window


My Controller has the following code:
BusinessLogic bl = new BusinessLogic();
SelectList slServerType = bl.CreateServerTypeSelectList(_repository, appcatalog_database.databaseServerTypeID);
ViewBag.databaseServerTypeID = slServerType;
return View(appcatalog_database);

Open in new window


The CreateServerTypeSelectList is defined as follows:
        public SelectList CreateServerTypeSelectList(IAppCatalogContainerRepository repository, int defaultServerType)
        {
            IEnumerable<AppCatalog_ServerType> db;
            db = repository.GetAllServerTypes();           

            SelectList sl;
            if (defaultServerType == 0)
                sl = new SelectList(db, "servertypeID", "servertypeName");
            else
                sl = new SelectList(db, "servertypeID", "servertypeName", defaultServerType);
            
            return sl;
        }

Open in new window


I verified after the list is created that the SelectedValue is 2 which is the same value as the databaseServerTypeID in my model.  (see attached picture)

My view has the following code for displaying the dropdown:
        <div class="editor-field">
            @Html.Label("Server Type", new { @class = "editor-label" }) :
            @Html.DropDownListFor(model => model.databaseServerTypeID, (SelectList)@ViewBag.databaseServerTypeID, "-- Choose Server Type --")
            <span class="error-msg">@Html.ValidationMessageFor(model => model.databaseServerTypeID)</span>
        </div>

Open in new window


I verified that the list in the ViewBag still has the SelectedValue set to 2 and it does (see attached picture).

I've reviewed other posts and it seems like I am doing everything correctly but I can't get the default value to be selected.  Any help is greatly appreciated!
CreateServerTypeSelectList.fw.png
CreateServerTypeSelectListinView.png
0
Comment
Question by:dyarosh
[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
3 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39979968
I am not sure what the problem is, but I am not a big fan of the ViewBag as a viable way to pass stuff to the view.

I prefer the strong-typed ViewModel over the dynamic ViewBag.  

Here is an article that talks about the ViewModel:

Part 3: Views and ViewModels
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-3
0
 

Accepted Solution

by:
dyarosh earned 0 total points
ID: 39989691
I finally found the problem.  The variable that I used in the ViewBag was the same name as the field in my model.  When I changed the ViewBag variable to ViewBag.ServerTypeList, the default value is getting set.  There must have been some confusion with the names being the same.
0
 

Author Closing Comment

by:dyarosh
ID: 39998544
I had not received any help from the Experts in a while so I kept trying different things until I finally got it to work.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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