Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:20:16+00:00 2026-06-09T21:20:16+00:00

I am trying to fix a bug in an existing application, and my MVC

  • 0

I am trying to fix a bug in an existing application, and my MVC ViewBag skills are limited. First off, I don’t have the time to refactor and change the code from a ViewBag to a ViewModel, although that would be my preferred approach.

I want to populate a DropDownList from an object in the ViewBag. The code is here:

if(Model != null && Model.WarehouseId != null && !Model.WarehouseId.Equals(String.Empty))                
{
    @Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes)<br /> //<---
}
else
{
    @Html.DropDownListFor(m => m.WarehouseId, (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, "")<br /> 
}

In the case that the WarehouseID is not String.Empty, I ONLY want to load that value into the DropDownList. I’m open to a C# or jQuery answer to this question.

Thanks in advance for any help offered!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:20:17+00:00Added an answer on June 9, 2026 at 9:20 pm

    and my MVC ViewBag skills are limited

    If you forget those skills it would be even better for you. Just kidding, they could be useful in order to know what not to do in an ASP.NET MVC application 🙂

    Anyway:

    if (Model != null && Model.WarehouseId != null && !Model.WarehouseId.Equals(String.Empty))                
    {
        @Html.DropDownListFor(
            m => m.WarehouseId, 
            ((IEnumerable<SelectListItem>)ViewBag.WarehouseCodes).Where(x => x.Value == Model.WarehouseId.Value)
        )
        <br />
    }
    else
    {
        @Html.DropDownListFor(
            m => m.WarehouseId, 
            (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, 
            ""
        )
        <br /> 
    }
    

    Another approach is to handle this case in your controller action:

    public ActionResult Index()
    {
        SomeModel model = ...
        IEnumerable<SelectItem> warehouseCodes = ...
        ViewBag.HasValue = false;
        if (model != null && model.WarehouseId != null && !string.IsNullOrEmpty(Model.WarehouseId.Value))
        {
            warehouseCodes = warehouseCodes.Where(x => x.Value == model.WarehouseId.Value);
            ViewBag.HasValue = true;
        }
        ViewBag.WarehouseCodes = warehouseCodes;
        return View(model);
    }
    

    and in your view you could now replace the if with:

    @Html.DropDownListFor(
        m => m.WarehouseId, 
        (IEnumerable<SelectListItem>)ViewBag.WarehouseCodes, 
        ViewBag.HasValue ? (string)null : ""
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fix a bug with a site search function and have isolated
We have a bug that we are trying to fix. It looks like one
I'm trying to fix a bug that I cannot reproduce (yipeee!). I have the
The following code shows a bug I'm trying to fix. I have two ways
I am trying to fix a bug on an ActiveX control. Is it possible
I've been trying to fix a bug in the Rails app I'm developing, and
Possible Duplicate: Java float division precision I'm trying to fix a bug in one
I'm trying to fix single quote bug in the code: std::string Index; connection->Open(); String^
I'm trying to fix a bug in VsClojure whereby Visual Studio does not recognize
I'm trying to fix a bug in this code that was made for windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.