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

  • Home
  • SEARCH
  • 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 9183749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:52:51+00:00 2026-06-17T18:52:51+00:00

I am trying to populate an ASP MVC3 drop down list with only two

  • 0

I am trying to populate an ASP MVC3 drop down list with only two select-able items and determine which item to select based on a value in a database. So far, this is what I have in my View (which I believe is correct)

    <div class="editor-label">
        @Html.LabelFor(model => model.Status)
    </div>
    <div class="editor-field">
        @Html.DropDownList("CategoryID")
    </div>

But I am running into a problem getting and setting the values in the controller.

First, the method I am using seems to only select the value in the drop down list according to which SelectListItem is listed first.

    public ActionResult Edit(int id)
    {
        //redirect if security is not met.  Must be admin here
        if (!Security.IsAdmin(User)) return RedirectToAction("Message", "Home", new { id = 1 });

        var item = db.FollowUpItems.Find(id);

        string start = string.Empty;

        if (item.Status.Equals("O")) start = "Open";
        else start = "Closed";

        ViewBag.CategoryID = new SelectList(new[]
                                                {
                                                    new SelectListItem {Text = "O", Value = "Open"},
                                                    new SelectListItem {Text = "C", Value = "Closed"},
                                                },"Text", "Value", start);

        FollowUpItems followupitems = db.FollowUpItems.Find(id);
        return View(followupitems);
    }

Second, when I hit the Save button on the edit page, the Status property of the FollowUpItem that gets passed into this part of the controller is always null.

    [HttpPost]
    public ActionResult Edit(FollowUpItems followupitems)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(followupitems).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (DbEntityValidationException dbEx)
        {
            ViewBag.ErrorMessage = Common.ErrorCheck.CombineDbErrors(dbEx);
        }
        catch (Exception ex)
        {
            ViewBag.ErrorMessage = ErrorCheck.FriendlyError(ex);
        }

        return View(followupitems);
    }

EDIT

The GET issue has been solved thanks to Quinton’s answer below, however I’m still having issues with setting the value. Using the @Html.DropDownList("CategoryID") method returns a null value to the controller everytime (for the Status value, all others are as they should be), and using the @Html.DropDownListFor(m => m.Status, ViewBag.CategoryID) syntax results in the following error message:

‘Compiler Error Message: CS1973: ‘System.Web.Mvc.HtmlHelper’ has no applicable method named ‘DropDownListFor’ but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.’

FINAL EDIT

The following cast in my View allowed me to properly set the values

@Html.DropDownListFor(m => m.Status, (SelectList)ViewBag.CategoryID)
  • 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-17T18:52:52+00:00Added an answer on June 17, 2026 at 6:52 pm

    Your item.Status field does contain an O or a C? If so then this may work, change the GET method to:

        public ActionResult Edit(int id) {
            //redirect if security is not met.  Must be admin here
            if (!Security.IsAdmin(User)) return RedirectToAction("Message", "Home", new { id = 1 });
    
            var item = db.FollowUpItems.Find(id);
    
            //string start = string.Empty;
    
            //if (item.Status.Equals("O")) start = "Open";
            //else start = "Closed";
    
            var StatusOptions = new [] { new { Text = "Open", Value = "O" }, new { Text = "Closed", Value = "C" }};
            ViewBag.CategoryID = new SelectList(StatusOptions, "Value", "Text", item.Status);
    
            /* this line is duplicated 
             */
            // FollowUpItems followupitems = db.FollowUpItems.Find(id);
            return View(item);
        }
    

    I don’t know what’s wrong with your @Html.DropDownList("CategoryID"), you may want to change it to:

    @Html.DropDownListFor(m => m.Status, (SelectList)ViewBag.CategoryID)
    

    I use it as i find it to be more expressive – i can cleary see what model property i want it to bind to.

    That may sort out your model binding issue. Just a note – before you can set the the Entity’s state, you must first attach it to the db context,

    db.Set<FollowUpItems>().Attach(followupitems);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to populate a drop down list in a view which led
I am trying to populate a dropdown list when some other drop down gets
I am trying to populate asp:DropDownList with data items and here is the code
ASP.Net MVC 4 I am trying to populate a list of Countries (data from
I have an asp:listbox which I populate from a database. I am however trying
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a List<Image> by reading the name of the each
I am trying to populate the list li from the LoginAction on to the
I'm trying to test the Data values that are returned from an ASP.NET MVC3
I'm trying to populate a dropdown list inside a repeater, but I'm not being

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.