Uzyskanie wielu wybranych wartości w Html.DropDownlistFor
@Html.DropDownListFor(m => m.branch, CommonMethod.getBranch("",Model.branch), "--Select--", new { @multiple = "multiple" })
@Html.DropDownListFor(m => m.division, CommonMethod.getDivision(Model.branch,Model.division), "--Select--", new { @multiple = "multiple" })
Mam dwa przypadki DropDownList. Chcę ustawić selected jako true dla tych, które wcześniej zapisały wartości dla modelu.oddział i Model.division. Są to tablice łańcuchowe przechowywanych identyfikatorów
class CommonMethod
{
public static List<SelectListItem> getDivision(string [] branchid , string [] selected)
{
DBEntities db = new DBEntities();
List<SelectListItem> division = new List<SelectListItem>();
foreach (var b in branchid)
{
var bid = Convert.ToByte(b);
var div = (from d in db.Divisions where d.BranchID == bid select d).ToList();
foreach (var d in div)
{
division.Add(new SelectListItem { Selected = selected.Contains(d.DivisionID.ToString()), Text = d.Description, Value = d.DivisionID.ToString() });
}
}
}
return division;
}
}
Zwrócona wartość podziału jest zaznaczona jako true dla wybranego elementu w modelu, ale po stronie widoku nie jest zaznaczona.
2 answers
Użyj ListBoxFor
zamiast DropDownListFor
:
@Html.ListBoxFor(m => m.branch, CommonMethod.getBranch("", Model.branch), "--Select--")
@Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), "--Select--")
Właściwości branch
i division
muszą być oczywiście kolekcjami, które będą zawierać wybrane wartości.
I pełny przykład właściwego sposobu tworzenia listy rozwijanej select multiple przy użyciu modelu widoku:
public class MyViewModel
{
public int[] SelectedValues { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
Które byłyby wypełnione w kontrolerze:
public ActionResult Index()
{
var model = new MyViewModel();
// preselect items with values 2 and 4
model.SelectedValues = new[] { 2, 4 };
// the list of available values
model.Values = new[]
{
new SelectListItem { Value = "1", Text = "item 1" },
new SelectListItem { Value = "2", Text = "item 2" },
new SelectListItem { Value = "3", Text = "item 3" },
new SelectListItem { Value = "4", Text = "item 4" },
};
return View(model);
}
I w widoku:
@model MyViewModel
...
@Html.ListBoxFor(x => x.SelectedValues, Model.Values)
Jest to helper HTML, który automatycznie wybierze pozycje, których wartości pasują do właściwości SelectedValues
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-29 11:40:54
Dla mnie działa również dla @Html.DropDownListFor
:
Model:
public class MyViewModel
{
public int[] SelectedValues { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
Kontroler:
public ActionResult Index()
{
var model = new MyViewModel();
// the list of available values
model.Values = new[]
{
new SelectListItem { Value = "2", Text = "2", Selected = true },
new SelectListItem { Value = "3", Text = "3", Selected = true },
new SelectListItem { Value = "6", Text = "6", Selected = true }
};
return View(model);
}
Razor:
@Html.DropDownListFor(m => m.SelectedValues, Model.Values, new { multiple = "true" })
Podane wartości SelectedValues w kontrolerze wyglądają następująco:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-02 14:45:37