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 the default option? For instance.

<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 is 4, but does not change the default setting 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 not set by a drop-down list in the HTTP POST.

Other information:

model.Item.Item.Status is an int. @Model.AllStatus is an SQL table listing all available state parameters.

+7
source share
4 answers

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

Will mean that for your case (in the action of the controller):

 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; 
+14
source

This is in addition to the answers above. Here is how I would do it.

A presentation model is designed to represent your data. So for one drop down, I will have the following:

 public class MyViewModel { public int StatusId { get; set; } public IEnumerable<Status> Statuses { get; set; } } 

And the Status class will look like this:

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

Controller action method for processing 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.

+5
source

I ended up using the thomasjaworski answer option.

View:

 @Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" }) 

ViewModel Constructor

  StatusSelectList = AllStatus.Select(x => new StatusSelectListItem { Text = x.Description, Value = x.id.ToString() }).ToList(); this.SelectedStatusIndex = 2;//Default Status is New 

Controller over HTTP POST

I install model.Item.Item.Status separately from the drop-down list itself:

 model.Item.Item.Status = model.SelectedStatusIndex; 

because the drop-down menu sets the value of the expression passed as the first argument:

 @Html.DropDownListFor(model => model.SelectedStatusIndex, new SelectList(@Model.StatusSelectList, "Value", "Text"), new { id = "statusDropdown" }) 

In this case, model.SelectedStatusIndex is what is given by the drop-down list. This controller implementation is what seemed complicated to me.

+3
source

You can use Paste to add the default Dropdown and add it to your dynamic list: This way, you do not need to use Razor in your view.

 List<Y> m = X.GetList(); m.Insert(0, new Y{ Name = "--Select--" }); 
0
source

Source: https://habr.com/ru/post/947738/


All Articles