Razor mutliselect下拉设置值,在ASP.NET MVC的多选下拉列表中选择 [英] Razor mutliselect dropdown- set values as selected in the multiselect dropdown in ASP.NET MVC
问题描述
我需要一种方法在Web应用程序的编辑视图中的多选下拉列表中设置已选择的值(ASP.NET,Mvc)
来自视图代码,我将选定的ID传递给选定的值数组。(当我从DB返回值时,将这些值分配给数组然后分配到这里。
我需要一个通过设置选择已选择的值来编辑视图。
请有人建议这样做
谢谢。
我尝试了什么:
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>
//Model
public int[] SelectedValues { get; set; }
//Controller
public ActionResult Edit(int id)
{
return View(GetDetails(id));
}
在GetDetails(id)中从DB获取值,并返回一个模型对象
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;
}
推荐答案
您需要将所选值传递给 MultiSelectList [ ^ ]构造函数。
使用ListBoxFor
方法也更为正确,因为你没有渲染下拉列表。 />
You need to pass the selected values to the MultiSelectList[^] constructor.
It would also be more correct to use theListBoxFor
method, since you're not rendering a drop-down list.
@Html.ListBoxFor(model => model.SelectedValues, new MultiSelectList(Models.GetCategoryList().OrderBy(c => c.Value), "Key", "Value", Model.SelectedValues), "Select Category", new { @class = "chzn-select form-control", multiple = "multiple" })
< a href =https://www.mikesdotnetting.com/article/265/asp-net-mvc-dropdownlists-multiple-selection-and-enum-support> ASP.NET MVC DropDownLists - 多项选择a和枚举支持 [ ^ ]
这篇关于Razor mutliselect下拉设置值,在ASP.NET MVC的多选下拉列表中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!