[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

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

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
        $(loader).show();
        
        //2.make ajax call to get json list
        $.ajax({
            url: action,
            cache: false,
            data: {},
            error: function (jqxhr, status, error) {
                console.log(jqxhr);
                $(select).css({ 'border-color': '#ff0000', 'background-color': 'rgba(255, 0, 0, .1)' });
                $(loader).hide();
            },
            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.text(item[textProp]);
                    op.data('object', item);
                    
                    $(select).append(op);

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


    };//end of dynamicDropDown

Client call:

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

            //callback
            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
bind
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,

Eileen

Leave a Reply