0

I created a controller which accepts one parameter from query string, this parameter is an integer, and i have a drop down list , i want to set a default value from the list based on the parameter value , but it's not working. I am using DropDownListFor html helper to create the list.

Here is my controller:

public ActionResult Index(int accountType)
        {
            List<SelectListItem> listItems = new List<SelectListItem>
            {
                new SelectListItem{Text = "Competitor",Value = "Competitor"},
                new SelectListItem{Text = "Consultant",Value = "Consultant"},
                new SelectListItem{Text = "Customer",Value = "Customer"},
                new SelectListItem{Text = "Investor",Value = "Investor"},
                new SelectListItem{Text = "Partner",Value = "Partner"},
                new SelectListItem{Text = "Influencer",Value = "Influencer"},
                new SelectListItem{Text = "Press",Value = "Press"},
                new SelectListItem{Text = "Prospect",Value = "Prospect"},
                new SelectListItem{Text = "Reseller",Value = "Reseller"},
                new SelectListItem{Text = "Supplier",Value = "Supplier"},
                new SelectListItem{Text = "Vendor",Value = "Vendor"},
                new SelectListItem{Text = "Other",Value = "Other"}
            };

            Account account = new Account
            {
                Name = "khaled",
                Email = "test",
                AccountType = listItems[accountType].Text
            };
            ViewBag.items = listItems;
            return View(account);
        } 

My View: @using System.Globalization @model Form_Task.Models.Account

<div class="form-group">
       @Html.Label("accounttype", "Account Type")
       @Html.DropDownListFor(Model=>Model.AccountType,(List<SelectListItem>)ViewBag.items,new{@class = "form-control", @disabled="disabled"})
</div>

My Model:

public class Account
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string AccountType { get; set;}
    }

If i change accountType to string and create the model like the following, it works fine:

Account account = new Account
                {
                    Name = "khaled",
                    Email = "test",
                    AccountType = accountType
                };
khaled rimawi
  • 55
  • 1
  • 8

1 Answers1

0

From your comment it sounds like you want to set the selected value based on its position in the list... If you must to that then this code before you pass it to the viewbag will work.

        Account account = new Account
        {
            Name = "khaled",
            Email = "test",
            AccountType = listItems[accountType].Text
        };

        listItems[accountType].Selected = true;//Add this line
        ViewBag.items = listItems;
        return View(account);

However this is not a good way to do things, what happens if the order changes? What happens if the int you get from the query string is larger than the length of listItems?

The more typical method is to set selected based on the value of your SelectListItem, something like this:

public ActionResult Index(string accountType)//pass through the listitems value instead of its position in the list.
{
    List<SelectListItem> listItems = new List<SelectListItem>
        {
            new SelectListItem{Text = "Competitor",Value = "Competitor"},
            new SelectListItem{Text = "Consultant",Value = "Consultant"},
            new SelectListItem{Text = "Customer",Value = "Customer"},
            new SelectListItem{Text = "Investor",Value = "Investor"},
            new SelectListItem{Text = "Partner",Value = "Partner"},
            new SelectListItem{Text = "Influencer",Value = "Influencer"},
            new SelectListItem{Text = "Press",Value = "Press"},
            new SelectListItem{Text = "Prospect",Value = "Prospect"},
            new SelectListItem{Text = "Reseller",Value = "Reseller"},
            new SelectListItem{Text = "Supplier",Value = "Supplier"},
            new SelectListItem{Text = "Vendor",Value = "Vendor"},
            new SelectListItem{Text = "Other",Value = "Other"}
        };

        //loop through and check for a match
        foreach (var item in listItems)
        {
            item.Selected = item.Value == accountType;
        }

        Account account = new Account
        {
            Name = "khaled",
            Email = "test",
            AccountType = listItems[accountType].Text
        };
        ViewBag.items = listItems;
        return View(account);
} 

This way, if the value doesn't match your query string it won't cause an exception and your method will still complete.

Edit - Just noticed you're using @Html.DropDownListFor(), change this to @Html.DropDownList() or just html5 control as otherwise Selected attribute is ignored.

<div class="form-group">
   @Html.Label("accounttype", "Account Type")
   <select name="AccountType" class="form-control" disabled="disabled">
       @foreach(var item in (List<SelectListItem>)ViewBag.items)
       {
       <option value='@item.Value' @(item.Selected ? "selected" : "")>@item.Text</option> 
       }
   </select>
</div>
Sean T
  • 2,414
  • 2
  • 17
  • 23