英文:
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_&#xA3;55" id="filter-group-Price--item-Up_to_&#xA3;55" />
<input class="js-filter-item" type="radio" name="Price" value="Up_to_&#xA3;70" id="filter-group-Price--item-Up_to_&#xA3;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_&#xA3;55" id="filter-group-Price--item-Up_to_&#xA3;55" />
<input class="js-filter-item" type="radio" name="Price" value="Up_to_&#xA3;70" id="filter-group-Price--item-Up_to_&#xA3;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>
}
输出:
英文:
> 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<RadioButtonModel> radioButtonModels)
{
return Ok(radioButtonModels);
}
View:
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_&#xA3;55" id="filter-group-Price--item-Up_to_&#xA3;55" />
<input class="js-filter-item" type="radio" name="Price" value="Up_to_&#xA3;70" id="filter-group-Price--item-Up_to_&#xA3;70" checked />
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 {
<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>
}
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论