Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不映射1个模型的情况下将2个复选框列表设置为1个表单_C#_Asp.net_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 如何在不映射1个模型的情况下将2个复选框列表设置为1个表单

C# 如何在不映射1个模型的情况下将2个复选框列表设置为1个表单,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,我创建了一些带有复选框列表的组件,例如: <form id="taskForm" asp-action="SetTasksView" asp-controller="Main" method="post" data-ajax="true" data-ajax-update="#taskList" data-ajax-loading="#spinner"

我创建了一些带有复选框列表的组件,例如:

<form id="taskForm" asp-action="SetTasksView" asp-controller="Main" method="post" data-ajax="true" data-ajax-update="#taskList" data-ajax-loading="#spinner" data-ajax-success="filterOnSuccess" data-ajax-mode="replace">
    @await Component.InvokeAsync("CategoryWidget", new { categories = ViewBag.UserFilter != null ? ViewBag.UserFilter.Categories : null })
    @await Component.InvokeAsync("ExchangesWidget", new { Exhanges = ViewBag.UserFilter != null ? ViewBag.UserFilter.Exchanges : null })
</form>

@wait Component.InvokeAsync(“CategoryWidget”,new{categories=ViewBag.UserFilter!=null?ViewBag.UserFilter.categories:null})
@wait Component.InvokeAsync(“ExchangesWidget”,new{Exhanges=ViewBag.UserFilter!=null?ViewBag.UserFilter.Exchanges:null})
其中包括:

<fieldset>
    <div class="wt-checkboxholder scroller" id="style-1">
        @for (int i = 0; i < Model.Count(); i++)
        {
            @Html.HiddenFor(m => m[i].Id)
            @Html.HiddenFor(m => m[i].Name)
            <span class="wt-checkbox">
                @Html.CheckBoxFor(m => m[i].Enabled, new { onclick = "CheckCheckBoxChecked()" })
                @Html.LabelFor(m => m[i].Enabled, Model[i].Name)
            </span>
        }
    </div>
</fieldset>

@对于(int i=0;im[i].Id)
@Html.HiddenFor(m=>m[i].Name)
@Html.CheckBoxFor(m=>m[i].已启用,新建{onclick=“checkboxchecked()”})
@Html.LabelFor(m=>m[i]。已启用,模型[i]。名称)
}
以及:


@对于(int i=0;ix.Id==Model[i].Id);}*@
@复选框($“[{Model[i].Id}].categories”,新的{Id=Model[i].Id})
@Label(“categories”,Model[i].Name,new{@for=Model[i].Id})
}
在控制器中,我有以下代码:

[HttpPost]
public async Task<IActionResult> SetTasksView(IEnumerable<ExchangeViewModel> ExchangeViewModels,
                                              OrderPriceViewModel priceModel,
                                              IEnumerable<CategoryViewModel> categories)
{
    var task = /// code

    return PartialView("_ListTask", tasks);
}
[HttpPost]
公共异步任务SetTasksView(IEnumerable ExchangeViewModels,
OrderPriceViewModel价格模型,
(可数范畴)
{
var task=///代码
返回PartialView(“\u ListTask”,tasks);
}

但当我按下第一个表单的按钮时,我得到的是ExchangeViewModels的唯一模型,为什么?如何拆分1个复选框和2个复选框?我想我缺少某种id来映射控制器中的传输,但我不知道我发现哪一个id可以为控制器中的模型设置前缀

[HttpPost]
public async Task<IActionResult> SetTasksView(IEnumerable<ExchangeViewModel> ExchangeViewModels,
                                          OrderPriceViewModel priceModel,
                                          [Bind(Prefix = "Category")] IEnumerable<CategoryViewModel> categories)
[HttpPost]
公共异步任务SetTasksView(IEnumerable ExchangeViewModels,
OrderPriceViewModel价格模型,
[Bind(Prefix=“Category”)]IEnumerable类别
我在复选框的名称中添加了前缀

<fieldset>
<div class="wt-checkboxholder scroller" id="style-1">
    @for (int i = 0; i < Model.Count(); i++)
    {
        @Html.Hidden($"Category[{i}].Id", Model[i].Id)
        <span class="wt-checkbox">
            @Html.CheckBox($"Category[{i}].Enabled", new { Id = Model[i].Id })
            @Html.Label("Category", Model[i].Name, new { @for = Model[i].Id })
        </span>
    }
</div>

@对于(int i=0;i
然后我可以将必要的数据放入不同的复选框列表中

<fieldset>
<div class="wt-checkboxholder scroller" id="style-1">
    @for (int i = 0; i < Model.Count(); i++)
    {
        @Html.Hidden($"Category[{i}].Id", Model[i].Id)
        <span class="wt-checkbox">
            @Html.CheckBox($"Category[{i}].Enabled", new { Id = Model[i].Id })
            @Html.Label("Category", Model[i].Name, new { @for = Model[i].Id })
        </span>
    }
</div>