创建单选按钮数组并传递给Net Core控制器

huangapple go评论79阅读模式
英文:

Create array of Radio buttons and pass to Net Core Controller

问题

以下是您要翻译的内容:

我试图将 JavaScript 中的单选按钮列表(数组)传递给 C# Net Core 6 控制器。

HTML 大致如下:

    <input class="js-filter-item" type="radio" name="Performance_Type" value="Standard" id="filter-group-Performance_Type--item-Standard"  />
    <input class="js-filter-item" type="radio" name="Performance_Type" value="VIP" id="filter-group-Performance_Type--item-VIP"  />
    
    <input class="js-filter-item" type="radio" name="Price" value="Up_to_£55" id="filter-group-Price--item-Up_to_£55"  />
    <input class="js-filter-item" type="radio" name="Price" value="Up_to_£70" id="filter-group-Price--item-Up_to_£70"  />


因此,有多个组(比我在这里显示的更多),每个组都有多个选项。我希望获得带有“name”、“value”和“checked”的选项的完整列表。或者如果更简单的话,只需带有“id”的“checked”选项列表即可。

到目前为止,我得到的最接近的结果是:

    var options = document.querySelectorAll('.js-filter-item');
    var optionsArray = JSON.stringify((Array.from(options).map(el => ([el.name, el.value, el.checked]))));


控制器上的参数定义为“string”。

然而,我得到的是无效的 JSON:

    [["Performance_Type","Standard",true],["Performance_Type","VIP",false]]

我觉得我可能没有正确的方法来处理这个问题!!基本上,在 C# 控制器中,我需要知道哪些选项被选中。

请注意,我已经为您提供了翻译,只包括了您想要的翻译内容,没有包含其他额外的内容。

英文:

I am trying to pass a list (array) of radio buttons from JavaScript to a C# Net Core 6 Controller.

The HTML is (something) like this:

<input class="js-filter-item" type="radio" name="Performance_Type" value="Standard" id="filter-group-Performance_Type--item-Standard"  />
<input class="js-filter-item" type="radio" name="Performance_Type" value="VIP" id="filter-group-Performance_Type--item-VIP"  />

<input class="js-filter-item" type="radio" name="Price" value="Up_to_£55" id="filter-group-Price--item-Up_to_£55"  />
<input class="js-filter-item" type="radio" name="Price" value="Up_to_£70" id="filter-group-Price--item-Up_to_£70"  />

So, there are a number of Groups (more than I've shown here), each with a number of options. I want to get either the full list of options with "name", "value" and "checked". Or if it's a lot easier, just a lit of "checked" options (with the "id" would do)

The closest I have got so far is:

var options = document.querySelectorAll('.js-filter-item');
var optionsArray = JSON.stringify((Array.from(options).map(el => ([el.name, el.value, el.checked]))));

With the parameter on the Controller defined as "string"

However, I am getting this, which isn't valid JSON:

[["Performance_Type","Standard",true],["Performance_Type","VIP",false]]

I feel like I'm not going about this the right way!! Basically, in the C# Controller, I need to know which options are checked.

答案1

得分: 0

Sure, here's the translated text:

我觉得我这样做可能不是正确的方式!!基本上,在C#控制器中,我需要知道哪些选项被选中。
实际上,这个情况可以通过多种方式实现,为了确定哪些单选按钮被选中,首先我们应该根据其Id设置条件。此外,我们需要将这些id绑定到类中,最后发送请求到控制器。

让我们来看看实际操作:假设我们有以下类。

模型:

public class RadioButtonModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public string Id { get; set; }
    public bool IsChecked { get; set; }
}

控制器:

[HttpPost]
public ActionResult PostRadioButton(List<RadioButtonModel> radioButtonModels)
{
    return Ok(radioButtonModels);
}

视图:

HTML:

<input class="js-filter-item" type="radio" name="Performance_Type" value="Standard" id="filter-group-Performance_Type--item-Standard" checked />
<input class="js-filter-item" type="radio" name="Performance_Type" value="VIP" id="filter-group-Performance_Type--item-VIP" />

<input class="js-filter-item" type="radio" name="Price" value="Up_to_£55" id="filter-group-Price--item-Up_to_£55" />
<input class="js-filter-item" type="radio" name="Price" value="Up_to_£70" id="filter-group-Price--item-Up_to_£70" checked />

**注意:**我已经在第一个和最后一个单选按钮中设置了checked属性以测试该场景。您需要根据Id或name属性在条件语句中进行操作。在线上有大量示例可以参考。

脚本:

@section scripts {
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
    <script>

        $(document).ready(function () {
            var options = document.querySelectorAll('.js-filter-item');
            var optionsArray = (Array.from(options).map(el => ([el.name, el.value, el.id, el.checked])));

            var listRadio = [];
            for (let i = 0; i < optionsArray.length; i++) {
                console.log("array: ", i, optionsArray[i]);
                var items = {
                    Name: optionsArray[i][0],
                    Value: optionsArray[i][1],
                    Id: optionsArray[i][2],
                    IsChecked: optionsArray[i][3],

                }
                listRadio.push(items);
            }
            console.log(listRadio);
            var url = '@Url.Action("PostRadioButton","CountOnModel")';
            $.ajax({
                url: url,
                type: "POST",
                datatype: "JSON",
                data: { radioButtonModels: listRadio },
                success: function (response) {
                    console.log(response);
                }
            });
        });
    </script>
}

输出:

创建单选按钮数组并传递给Net Core控制器

创建单选按钮数组并传递给Net Core控制器

英文:

> I feel like I'm not going about this the right way!! Basically, in the
> C# Controller, I need to know which options are checked.

Actually, this scenario can be achieve through numerous way and in order to sort which radio buttons are checked, first of all we should set conditionals based on its Id. In addition, we need bind those ids into class and Finally, send request to the controller.

Lets have a look in practice: assuming we have following class.

Model:

public class RadioButtonModel
    {
     
        public string Name { get; set; }
        public string Value { get; set; }
        public string Id { get; set; }
        public bool IsChecked { get; set; }
    }

Controller:

    [HttpPost]
    public ActionResult PostRadioBUtton(List&lt;RadioButtonModel&gt; radioButtonModels)
    {
        return Ok(radioButtonModels);
    }

View:

HTML:

&lt;input class=&quot;js-filter-item&quot; type=&quot;radio&quot; name=&quot;Performance_Type&quot; value=&quot;Standard&quot; id=&quot;filter-group-Performance_Type--item-Standard&quot; checked /&gt;
&lt;input class=&quot;js-filter-item&quot; type=&quot;radio&quot; name=&quot;Performance_Type&quot; value=&quot;VIP&quot; id=&quot;filter-group-Performance_Type--item-VIP&quot; /&gt;

&lt;input class=&quot;js-filter-item&quot; type=&quot;radio&quot; name=&quot;Price&quot; value=&quot;Up_to_&amp;#xA3;55&quot; id=&quot;filter-group-Price--item-Up_to_&amp;#xA3;55&quot; /&gt;
&lt;input class=&quot;js-filter-item&quot; type=&quot;radio&quot; name=&quot;Price&quot; value=&quot;Up_to_&amp;#xA3;70&quot; id=&quot;filter-group-Price--item-Up_to_&amp;#xA3;70&quot; checked /&gt;

Note: I have set checked attribute in first and last radio button to test the scenario. You have to do it in if conditional based on Id or name attribute. There are tons of sample onlin how to do that.

Script:

@section scripts {
    &lt;script src=&quot;https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;

        $(document).ready(function () {
            var options = document.querySelectorAll(&#39;.js-filter-item&#39;);
            var optionsArray = (Array.from(options).map(el =&gt; ([el.name, el.value, el.id, el.checked])));
       
            var listRadio = [];
            for (let i = 0; i &lt; optionsArray.length; i++) {
                console.log(&quot;array: &quot;, i, optionsArray[i]);
                    var items = {
                        Name: optionsArray[i][0],
                        Value: optionsArray[i][1],
                        Id: optionsArray[i][2],
                        IsChecked: optionsArray[i][3],
                       
                       

                    }
                    listRadio.push(items);
            }
            console.log(listRadio);
            var url = &#39;@Url.Action(&quot;PostRadioBUtton&quot;,&quot;CountOnModel&quot;)&#39;;
            $.ajax({
                url: url,
                type: &quot;POST&quot;,
                datatype: &quot;JSON&quot;,
                data: { radioButtonModels: listRadio },
                success: function (response) {
                    console.log(response);
                }
            });
        });
    &lt;/script&gt;
    }

Output:

创建单选按钮数组并传递给Net Core控制器

创建单选按钮数组并传递给Net Core控制器

huangapple
  • 本文由 发表于 2023年5月14日 21:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定