简体   繁体   English

如何在asp.net中使用select2返回下拉列表的值?

[英]How to return a value/s of a dropdownlist using select2 in asp.net?

I am currently learning front-end development in asp.net and I seem to be lacking some fundamental understanding when it comes down to dropdownlists in particular. 我目前正在学习asp.net的前端开发,特别是当涉及到下拉列表时,我似乎缺乏一些基本的理解。 My goal is what I thought should be very straight forward and simple - to produce a multi-select, Select2 dropdownlist which posts back the values of the chosen select list items. 我的目标是我认为应该非常直接和简单 - 生成一个多选的Select2下拉列表,它回发所选择的选择列表项的

For some reason I simply cannot figure out how to do this, despite reading MS documents and similar StackOverflow questions. 出于某种原因,我无法弄清楚如何做到这一点,尽管阅读了MS文档和类似的StackOverflow问题。 I can produce the dropdown, select the items, and even return a single value. 我可以生成下拉列表,选择项目,甚至返回单个值。 But I can't get the value of the item/s before submitting (for js processing) so that I can return a list (or concatinated string) of all selected values. 但是在提交之前我无法获得item / s的值(对于js处理),这样我就可以返回所有选定值的列表(或连续字符串)。

My code is below, you can see in the javascript I have been attempting to access the value/s in different ways, but the value doesn't even get set on the li in the inspect element. 我的代码如下,你可以在javascript中看到我一直试图以不同的方式访问值/ s,但是这个值甚至没有在inspect元素中的li上设置。

It would be great if someone could explain how to form this dropdownlist correctly so that I can retrieve the values I need (aka "80" and "81"). 如果有人能够解释如何正确地形成这个下拉列表,那么我可以检索我需要的值(又名“80”和“81”)。

Controller 调节器

public ActionResult About()
        {
            AboutViewModel viewModel = new AboutViewModel();

            viewModel.Movies = new List<SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "Honey I Shrunk The Kids",
                    Value = "80"
                },
                new SelectListItem()
                {
                    Text = "Shrek 2",
                    Value = "81"
                }
            };
            return View(AboutViewPage, viewModel);
        }

[HttpPost]
public ActionResult About(AboutViewModel viewModel)
{
    return View();
}


View 视图

   <div>
        @using (Html.BeginForm("About", "Home"))
        {

            @Html.DropDownList("Result", new SelectList(Model.Movies, "Value","Text"), new { @class = "values js-example-basic-single", @multiple = "multiple", @Id="movies" });

            <input type="submit" value="Submit" class="submit" onclick="collectMovies();" />
        }

    </div>



    <script>
        function collectMovies() {
            var test1= $('.select2-selection__choice').val();
            var testString = "";

            for (var i = 0; i < test1.length; i++) {
                testString += test1[i];
            }
        }


        $(document).ready(function () {
            $('.js-example-basic-single').select2();
        });
    </script>

When a multiple select control is posted to the server, the collection of selected values are processed by ASP.NET into a comma-separated list. 当多重选择控件发布到服务器时,ASP.NET将处理选定值的集合到逗号分隔列表中。 The MVC model binding system quite happily sees this collection as an array. MVC模型绑定系统非常高兴地将此集合视为一个数组。 If the selected values can be parsed into ints, you can use an int array to represent the posted values 如果可以将所选值解析为整数,则可以使用int数组来表示已发布的值

public class AboutViewModel 
{
   public int[] Result {get;set;}
}

[HttpPost]
public ActionResult About(AboutViewModel viewModel)
{
    return View();
}

Source 资源

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

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