Share via


Preselect dropdown select option

Question

Tuesday, August 16, 2011 10:23 AM

Hi,

I have some code that inserts the value of a dropdown into th database. However when coming back to the page, it doesn't preselet the option that was selected before the update.

My View Model is like so:

        //Dropdown options
        private string _selectedIncoTerm;

        public IList<BTBLL.INCOTerm> IncoTerms { get; set; }

        public string SelectedIncoTerm
        {
            get
            {
                if (String.IsNullOrWhiteSpace(_selectedIncoTerm) && IncoTerms != null)
                {
                    _selectedIncoTerm = IncoTerms.First(x => x.DefaultOption).Code;
                }

                return _selectedIncoTerm;
            }

            set { _selectedIncoTerm = value; }
        }

In my Controller action i do this to set the value:

IncoTerms = _incoTerms.FindAll()

Then in my view i have this:

@Html.DropDownListFor(x => x.SelectedIncoTerm, incoTypes, null)

Can you see anything obvious that i am doing wrong?

Many thanks,

All replies (2)

Tuesday, August 16, 2011 2:21 PM ✅Answered

See here:

http://stackoverflow.com/questions/4800447/asp-net-mvc-3-pre-select-html-dropdownlistfor-not-working-in-nerd-dinner

http://stackoverflow.com/questions/6807256/dropdownlist-set-selected-value-in-mvc3-razor


Thursday, August 18, 2011 4:49 AM ✅Answered

Hi billy

In addtion raduenuca, you also should check this link.

http://stackoverflow.com/questions/589935/html-dropdownlist-in-asp-net-mvc-rc-refresh-not-pre-selecting-item

Hope this helpful.

Regards

Young Yang