[RESOLVED]Populate dropdown with data

I have a list coming back, but I want a dropdown to has the current value(author) selected and the rest of the authors in the dropdown. This is what I have so far

    @Html.DropDownList("Authorname", Model.Select(m => new SelectListItem() { Text = m.Authorname, Value = m.Authorname} ).ToList())

Are you looking to do this on the Server or the client. I use ajax here is an example:

Example of repository code using EF:

public List<Part> GetParts()
            _db = new PNAContext();

            return (from p in _db.Parts
                    where p.Deleted == false
                    orderby p.PartNumber
                    select p).ToList();

        }//end of GetParts()

Then in my controller:

        public ActionResult GetPartsJson()
            return Json(_repo.GetParts(), JsonRequestBehavior.AllowGet);

        }//end of GetPartsJson()

Library JavaScript code:

//function to bind a Json array to a dropdown
    var dynamicDropDown = function (action, select, loader, valueProp, textProp, initialText, initialVal, callback) {

        //1.show loader
        //2.make ajax call to get json list
            url: action,
            cache: false,
            data: {},
            error: function (jqxhr, status, error) {
                $(select).css({ 'border-color': '#ff0000', 'background-color': 'rgba(255, 0, 0, .1)' });
            success: function (collection) {
                //3.add initial text
                $(select).html('<option value="' + initialVal + '">' + initialText + '</option>');
                //4.Add each item to the drop down
                $.each(collection, function (i, item) {

                    var op = $('<option/>', { value: item[valueProp] });
                    op.data('object', item);

                });//end of each
                //5.hide loader
                //6.Invoke callback
                if (typeof (callback) === 'function') {
        });//end of ajax call

    };//end of dynamicDropDown

Client call:

pro.dynamicDropDown('/data/GetPartsJson', '#parts_drop_down', '#profile_parts_loader',
            'Id', 'PartNumber', 'select part', '',

            function (collection) {

            });//end of dynamicdropdown parts

Hope it helps!

Use HtmlDropDownListFor. Please take a look at this blog

How to simple Html.DropDownListFor MVC.NET

If you still have question, please provide additional details on what is the data type of Model, where is your list, where is the default authorname stored etc.

Hi helixpoint,

Thanks for your post.

There is a lot of ways to
dropdownlist in asp.net mvc (mvc3, mvc4 or later razor syntax .cshtml) and maintaining dropdownlist state during postback some of them given below -

  • Use viewbag property.
  • Through view model.
  • Use jquery .ajax i.e. asynchronous post back at selected index change of another dropdownlist.

More information:

binding dropdownlist in .net mvc razor by viewbag, model and jquery – 3 ways

Hope this can be helpful.

Best Regards,


Leave a Reply