0

I have a dropdownlist. Its not required, so that backing field is nullable. But, when the form loads, the dropdownlist seems to be setting the selected value to the first id (first sequentially).

I build the select list items like this:

var servers = await Context.Servers.OrderBy(s => s.Name).ToListAsync();
servers.Insert(0, new Models.Server() { Id = Guid.Empty, Name = "SELECT ONE" });
return servers.Select(t => new SelectListItem()
{
    Selected = t.Id == Guid.Empty ? true : false,
    Text = t.Name,
    Value = t.Id.ToString()
}); 

And I render it like this:

@Html.DropDownListFor(m => m.ProductionEnvironment_ServerId, Model.ProductionEnvironment_Servers)

The first sequential Guid in the ID field is selected if ServerId is null. If I make ServerId non-nullable, the drop down becomres required, which it should not be.

1
  • Are you sure that none of the servers has an Id equal to Guid.Empty? Furthermore, you could make a slight change Selected=t.Id == Guid.Empty. If you do so you will have the same result. Commented Nov 3, 2015 at 19:23

1 Answer 1

1

In your GET action method, Set your view model'sProductionEnvironment_ServerId property value to the one you want to be selected when the dropdown is rendered.

public ActionResult Edit()
{
  var vm = new YourViewModel(int id);
  vm.ProductionEnvironment_Servers = servers.Select(s => new SelectListItem
            {
                Value = s.Id.ToString(),
                Text = s.Name
            });

  //Set the value you want to be selected.
 Guid guidWhichShouldBeSelected = GetSelectedGuidHereFromSomeWhere(id);

 vm.ProductionEnvironment_ServerId = guidWhichShouldBeSelected;
 return View(vm);
}

Also, in the razor view, you can use this override of the helper method to add a "Select" option to the option list. You don't need to add one from server side.

@Html.DropDownListFor(m => m.ProductionEnvironment_ServerId, 
                                            Model.ProductionEnvironment_Servers,"Select")

This will add "Select" option as the first option.

When you submit the form, In your HttpPost action method, check the value of ProductionEnvironment_ServerId property of the posted model. You will get the selected Item's value (a Guid) or null if they select the first "Select" option.

[HttpPost]
public ActionResult Edit(YourViewModel model)
{
  //check model.ProductionEnvironment_ServerId 
  // to do : Save and Redirect
}
Sign up to request clarification or add additional context in comments.

5 Comments

The server id may be null, because its optional. Also, if I use the method you mentioned for creating the "Select" option, there is no value for that option, so I couldnt set it to that option anyway
@EthanSchofer It will be null when you post the form, which is the correct way. See my updated answer.
OK, maybe Im being dense... on this line: //Set the value you want to be selected. Guid guidWhichShouldBeSelected = GetSelectedGuidHereFromSomeWhere(id); What do I set it to. The value for my 'SELECT ONE' option is blank. <option value>SELECTONE</option>
@EthanSchofer If you are showing an edit screen (ex : Edit customer) You might want to show previously saved value as the selected item in the dropdown. So read it from your db and use it. If it is an "Addn New" screen, you might not want to show any options as selected" .Then don't execute that line. The overload i provided will add the default "Select" to your dropdown.
OK, somehow, I have three dropdownlists and I have somehow tied them all to the same list of selectlistitems. They are all getting set at once.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.