简体   繁体   English

将所选项目分配给更新“多个”的下拉列表

[英]Assigning selected items to drop-down list on update "multiple"

I have an HTML.dropdown multiple select2 working perfect.我有一个 HTML.dropdown多个 select2工作完美。 When I save values,当我保存值时,

BUT: On update page I have to show the pre selected values in the dropdown但是:在更新页面上,我必须在下拉列表中显示预选的值

here is the code:这是代码:

<div class="col-md-6 mb-3" id="categorylist">
   <p class="mb-1 font-weight-bold text-muted mt-3 mt-md-0">Category*</p>
   @Html.DropDownList("pCategory[]", new SelectList(new admin.Models.CategoryModel().getMultipleCategoryBySP(), "cat_id", "cat_name", --placeToProvideSingleIntValue--), 
     new { @class = " form-control select2-multiple ", @data_toggle = "select2", @multiple = "multiple", @style = "width: 100%;" })
</div>

in above code, there is a place holder --placeToProvideSingleIntValue-- where I can place single integer value it shows as preSelected.在上面的代码中,有一个占位符--placeToProvideSingleIntValue--我可以在其中放置单个整数值,它显示为 preSelected。

Solution/HELP Required for: i want to pass an array to it or multiple values anyother way.解决方案/帮助需要:我想以任何方式将数组或多个值传递给它。 so it would show multiple pre selected values.所以它会显示多个预选值。

You'll have to use a MultiSelectList instead of a SelectList.您必须使用MultiSelectList而不是 SelectList。 Something like就像是

@Html.DropDownList("pCategory[]", new MultiSelectList(new admin.Models.CategoryModel().getMultipleCategoryBySP(), "cat_id", "cat_name", --placeToProvideMultipleIntValue--), 
     new { @class = " form-control select2-multiple ", @data_toggle = "select2", @multiple = "multiple", @style = "width: 100%;" })

Razor-pages(near the Blazor thingy :D) is one of the newest and most modern frameworks from Microsoft for now, so I think you should try to use tag helpers wherever is possible as you can get so many benefits from using it! Razor-pages(靠近 Blazor 的东西:D)是目前微软最新、最现代的框架之一,所以我认为你应该尽可能地使用标签助手,因为你可以从使用中获得很多好处! You can have a look at the sample here您可以在此处查看示例

So the tag component on your .cshtml page should look quite simple, like:所以 .cshtml 页面上的标签组件应该看起来很简单,比如:

<select asp-for="pCategory" asp-items="items" multiple class="form-control select2-multiple" style="width: 100%;" data_toggle = "select2"></select>

where an item is a MultiSelectList object其中一个项目是一个MultiSelectList对象

MultiSelectList items = new MultiSelectList(Categories, "cat_id", "cat_name", 
selectedValues);

and selectedValues array of ints.selectedValues整数数组。 Hope it will help :)希望它会有所帮助:)

And just one small thing out of the scope.而且只是超出范围的一件小事。 I don't think it is nice to do something like this我不认为做这样的事情很好

new admin.Models.CategoryModel().getMultipleCategoryBySP()

on your client-side.在您的客户端。 I believe that the right thing that should be done is to pass just a flat object to the client-side and keep the whole business logic/conversions/mapping stuff in the back-end.我认为应该做的正确的事情是将一个平面对象传递给客户端,并将整个业务逻辑/转换/映射内容保留在后端。

Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM