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 6186267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:51:25+00:00 2026-05-24T01:51:25+00:00

Here is my model: public class NewsCategoriesModel { public int NewsCategoriesID { get; set;

  • 0

Here is my model:

public class NewsCategoriesModel {
    public int NewsCategoriesID { get; set; }        
    public string NewsCategoriesName { get; set; }
}

My controller:

public ActionResult NewsEdit(int ID, dms_New dsn) {
    dsn = (from a in dc.dms_News where a.NewsID == ID select a).FirstOrDefault();
    var categories = (from b in dc.dms_NewsCategories select b).ToList();
    var selectedValue = dsn.NewsCategoriesID;
    SelectList ListCategories = new SelectList(categories, "NewsCategoriesID", "NewsCategoriesName",selectedValue);

    // ViewBag.NewsCategoriesID = new SelectList(categories as IEnumerable<dms_NewsCategory>, "NewsCategoriesID", "NewsCategoriesName", dsn.NewsCategoriesID);
    ViewBag.NewsCategoriesID = ListCategories;
    return View(dsn);
}

And then my view:

@Html.DropDownList("NewsCategoriesID", (SelectList)ViewBag.NewsCategoriesID)

When i run, the DropDownList does not select the value I set.. It is always selecting the first option.

  • 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-05-24T01:51:26+00:00Added an answer on May 24, 2026 at 1:51 am

    You should use view models and forget about ViewBag Think of it as if it didn’t exist. You will see how easier things will become. So define a view model:

    public class MyViewModel
    {
        public int SelectedCategoryId { get; set; }
        public IEnumerable<SelectListItem> Categories { get; set; } 
    }
    

    and then populate this view model from the controller:

    public ActionResult NewsEdit(int ID, dms_New dsn)
    {
        var dsn = (from a in dc.dms_News where a.NewsID == ID select a).FirstOrDefault();
        var categories = (from b in dc.dms_NewsCategories select b).ToList();
    
        var model = new MyViewModel
        {
            SelectedCategoryId = dsn.NewsCategoriesID,
            Categories = categories.Select(x => new SelectListItem
            {
                Value = x.NewsCategoriesID.ToString(),
                Text = x.NewsCategoriesName
            })
        };
        return View(model);
    }
    

    and finally in your view use the strongly typed DropDownListFor helper:

    @model MyViewModel
    
    @Html.DropDownListFor(
        x => x.SelectedCategoryId,
        Model.Categories
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my model public class Movie { public int MovieID { get; set; }
I have a model public class Foo{ public int Id{get;set;} public string Name {get;
Here is my model public class Horse { public int HorseId { get; set;
Model: public class Model { public ItemType Type { get; set; } public int
I have this model: public class Package { public string CustomerName { get; set;
Here is a model: Public class Person { [Key] Public int PersonId { get;
I have following model: public class FormularModel { [Required] public string Position { get;
Consider the following model: public class BandProfileModel { public BandModel Band { get; set;
ok say i have the following model: public class bar{ public string bar {get;
Here are the domain model classes: public abstract class BaseClass { ... } public

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.