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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:53:11+00:00 2026-06-12T19:53:11+00:00

I have a generic list method that returns a CategoryID and CategoryName. I have

  • 0

I have a generic list method that returns a CategoryID and CategoryName.
I have spent enough time researching and cant seem to put it together. I very new at MVC.

Here is my DropdownList Method in a repository. I get back the data… So far so good.

public List<DropdownList> GetDDl()
{

return catDDL; 
}

Here is my CONTROLLER CODE(attempt at it)

   IEnumerable<SelectListItem> liCat =
 userRepository.Getddl().Select(c => new SelectListItem
{
   Value = c.DropDownID.ToString(),
   Text = c.DropDownText
}
ViewBag.catItems = new SelecList(liCat,"Value","Text");

Here is my VIEW

@Html.Dropdownlist("catItems","Select Category)
  • 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-12T19:53:12+00:00Added an answer on June 12, 2026 at 7:53 pm

    Try to avoid dynamic stuff like ViewBag and ViewData. Use strongly typed views.

    ViewModel is just a POCO class which we will use to transfer data between your view and the action method. It will be specific to the view.

    ex : if you want to create a view which creates a product. So create a viewmodel like this

    public class Product
    {
      public string Name { set;get;}
      public IEnumerable<SelectListItem> Categories{ get; set; }
      public string SelectedCategoryId { get; set; }
      //Other Properties as needed
    
    }
    

    now in your GET action method, you create an object of this view model and initialize the values and send to the view.

    public ActionResult Create()
    {
      var vm=new Product();
      vm.Categories=userRepository.Getddl().
                   Select(c => new SelectListItem
                                     {
                                        Value = c.DropDownID.ToString(),
                                        Text = c.DropDownText
                                     });                    
      return View(vm);
    }
    

    Now make your view strongly typed to our Product class and use the Html.DropDownListFor helper method.

    @model PersonsProduct 
    @using (Html.BeginForm())
    { 
      @Html.DropDownListFor(x => x.SelectedCategoryId, 
                           new SelectList(Model.Categories,"Value","Text"), "Select")
      <input type="submit" value="save" />
    }
    

    Now in your HttpPost , you can get the form values like this

    [HttpPost]
    public ActionResult Create(Product model)
    {
      if(ModelState.IsValid)
      {
         //check model.SelectedCategoryId
         //save and redirect
      }
      //to do :reload the dropdown again.
      return view(model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webservice that calls a method that returns a generic list of
I have a webservice that calls a method and returns a generic list. The
I have a generic Print method that iterates over a list and simply prints
I have a base class that has an abstract method which returns a list
I have a method which returns a generic list (from the db it returns
I have a generic linked-list that holds data of type void* I am trying
As an exercise I'm trying to create a function that returns a generic list
I currently have a Generic method that reads an Value from the database based
I have a data tier select method that returns a datatable. It's called from
I have the following method that calculates the top 20 numbers in a list

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.