I need a way to set already selected values in a multi select dropdown in a edit view in a web application (ASP.NET, Mvc)
from view code,I passed selected ids in to selected values array.(when I return values from DB , assign those in to a array then assign to here.
I need a edit view by set selecting already selected values.
please anyone suggest way to do this
Thank you.
What I have tried:
//View
<div class="form-group">
<label>Choose Categories</label>
@Html.DropDownListFor(model => model.SelectedValues, new MultiSelectList(Models.GetCategoryList().OrderBy(c => c.Value), "Key", "Value"), "Select Category", new { @class = "chzn-select form-control", multiple = "multiple" })
</div>
public int[] SelectedValues { get; set; }
public ActionResult Edit(int id)
{
return View(GetDetails(id));
}
In GetDetails(id) take values from DB , and return a model object
public GetDetails()
{
int[] arr = new int[2];
arr[0] = 4;
arr[1] = 5;
modelObj.SelectedValues = arr;
.......
......
return modelObj;
}