简体   繁体   English

如何从asp中的javascript jquery中获取选定的值,以便在C#后面的代码中使用它

[英]how to get selected values from javascript jquery in asp to use it in the code behind of C#

I'm trying to get the selected values from below multiselect jquery to use it in the code behind in C#. 我正在尝试从多选jquery下面获取所选值,以便在C#后面的代码中使用它。 I got the script sources from below: 我从下面得到了脚本来源:

https://github.com/nobleclem/jQuery-MultiSelect

I'm using the code in asp as follow: 我正在使用asp中的代码如下:

<div class="col-sm-3">
                            <label class="text-light-blue">MultiSelect Codes</label>
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <i class="fa fa-globe"></i>
                                </div>
                                <select id="test" name="basic[]" multiple="multiple">
                                    <option value="1">1 Code</option>
                                    <option value="2">2 Code</option>
                                    <option value="3">3 Code</option>
                                    <option value="4">4 Code</option>
                                    <option value="70">70 Code </option>
                                    <option value="80">80 Code </option>
                                    <option value="90">90 Code </option>
                                    <option value="88">88 Code </option>
                                    <option value="99">99 Code </option>
                                </select>
                            </div>
                        </div>


                        <script>
                        $(function () {
                                    $('select[multiple]').multiselect({
                                        columns: 1,
                                        search: true,
                                        searchOptions: {
                                            showOptGroups: true,
                                            searchText: true,
                                            searchValue: true,
                                        },
                                        texts: {
                                            placeholder: 'Select Codes', search: 'Search',
                                            selectedOptions: ' selected',
                                            selectAll: 'Select all',
                                            unselectAll: 'Unselect all',
                                        },
                                        selectAll: true, // add select all option
                                        selectGroup: true, // select entire optgroup
                                        minHeight: 250,   // minimum height of option overlay
                                        maxHeight: 250,  // maximum height of option overlay
                                        maxWidth: 250,  // maximum width of option overlay (or selector)
                                        maxPlaceholderWidth: 250, // maximum width of placeholder button
                                        maxPlaceholderOpts: 250, // maximum number of placeholder options to show until "# selected" shown instead
                                        showCheckbox: true,  // display the checkbox to the user
                                        onOptionClick: function (element, option) { }, // fires when an option is clicked

                                    });
                                       });
                    </script>

I'm expecting to return the selected values from above options in C#, for example as : 我希望在C#中从上面的选项中返回选定的值,例如:

2,4,99 2,4,99

Edited 编辑

You can post the selected values to a controller action like this: 您可以将选定的值发布到控制器操作,如下所示:

var selectedItems = $('select#test').val()
$.ajax({
    url: '@Url.Action("SaveSelectedCodes", "MyController")',
    type: 'POST',
    cache: false,
     data: JSON.stringify(selectedItems),
    success: function (result) {
        $('#myDiv').html(result);
    }
});

Assuming you use .net mvc you would have a controller with a action where you would receive the values. 假设你使用.net mvc你将有一个controller一个action ,你会得到的值。

public class MyController : Controller
{

    [HttpPost]
    public ActionResult SaveSelectedCodes( string[] SelectedCodes)
    {
        if (SelectedCodes != null)
        {
            string codeValue = string.Join("," , SelectedCodes);

            //
            // write your code to save into database
            //
        }
        return View("Index"));
    }
}

You can try in this way to get the values of the selected options/items: 您可以尝试以这种方式获取所选选项/项的值:

$('select[multiple]').val()

Thanks 谢谢

You will need an ASP control and have jQuery update the ASP control with the new values. 您将需要一个ASP控件并让jQuery使用新值更新ASP控件。

ASP ASP

<asp:HiddenField ID="HiddenField1" ClientIDMode="Static" runat="server" />

JQuery JQuery的

(function(){
   $('#HiddenField1').val('[new values]');
})();

C# C#

var myNewValues = HiddenField1.Value;

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

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