Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DropDownListFor from SelectList with default value

I have a dropdownlistfor:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
 @Html.ValidationMessageFor(model => model.Item.Item.Status)

HTML output:

<select id="statusDropdown" class="valid" name="Item.Item.Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">
<option value="2">Completed by Admin</option>
<option value="3">General Error</option>
<option value="4">New</option>
</select>

How can I update this code to set a default selected option? E.G.

<option value="4" selected>New</option>

I tried:

 @Html.DropDownListFor(model => model.Item.Item.Status, new SelectList(@Model.AllStatus, "id", "Description",@Model.SelectedStatusIndex), new { id = "statusDropdown" })

@Model.SelectedStatusIndex has a value of 4, but does not change the default option to New.

I also tried:

@Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.AllStatus, "id", "Description"), new { id = "statusDropdown" })
@Html.ValidationMessageFor(model => model.Item.Item.Status)

This selects the default option "New", but model.Item.Item.Status is not set by the dropdown on HTTP POST.

Other Detail:

model.Item.Item.Status is an int. @Model.AllStatus is a SQL table that lists all available status options.

like image 715
P.Brian.Mackey Avatar asked Jun 20 '13 13:06

P.Brian.Mackey


2 Answers

There exist already some discussions about that here or there. One of the problems might be using a different type than string for the key value. I had similar problems in past and I know that i solved it like this - explicitly setting the Selected property when preparing the list (in your case, AlLStatus).

Would mean, for your case (in controller action):

IEnumerable<SelectListItem> selectList = 
from s in allStatus // where ever you get this from, database etc.
select new SelectListItem
{
    Selected = (s.id == model.Item.Item.Status),
    Text = cs.Description,
    Value = s.id.ToString()
};
model.AllStatus = selectList;
like image 60
thmshd Avatar answered Oct 21 '22 06:10

thmshd


This is in addition to the answers above. This is how I would have done it.

The view model is there to represent your data. So for a single drop down list I would have the following:

public class MyViewModel
{
     public int StatusId { get; set; }

     public IEnumerable<Status> Statuses { get; set; }
}

And the Status class would look like this:

public class Status
{
     public int Id { get; set; }

     public string Description { get; set; }
}

The controller's action method to handle the view:

public class MyController
{
     private readonly IStatusService statusService;

     public MyController(IStatusService statusService)
     {
          this.statusService = statusService;
     }

     public ActionResult MyActionMethod()
     {
          MyViewModel viewModel = new MyViewModel
          {
               Statuses = statusService.GetAll(),
               StatusId = 4 // Set the default value
          };

          return View(viewModel);
     }
}

The view will look like this:

@model MyProject.ViewModels.MyViewModel

@Html.DropDownListFor(
     x => x.StatusId,
     new SelectList(Model.Statuses, "Id", "Description", Model.StatusId),
     "-- Select --"
)
@Html.ValidationMessageFor(x => x.StatusId)

There you go.

like image 34
Brendan Vogt Avatar answered Oct 21 '22 05:10

Brendan Vogt