use*_*879 12 asp.net-mvc model-binding razor
我有一个项目列表,我想删除复选框列表中选中的项目.
我不能使用像CheckboxList
我Grid.Mvc
用来显示我的线条的东西.这就是我在每行中创建复选框的原因column.add("<input type="checkbox".....>);
.
每个复选框都有自己的ID:
<input type="checkbox" id="3">
<input type="checkbox" id="4">...
Run Code Online (Sandbox Code Playgroud)
我想知道如何将所有选中的复选框ID传递给控制器(从那里我将执行删除操作).如何通过一键按下从表单中将已检查ID的数组发布到我的控制器操作?
Jer*_*ook 37
生成的HTML示例:
<label><input type="checkbox" name="deletedItems" value="3"> Some label for 3</label>
<label><input type="checkbox" name="deletedItems" value="4"> Some label for 4</label>
...
<button type="submit">Submit</submit>
Run Code Online (Sandbox Code Playgroud)
控制器动作:
[HttpPost]
public ActionResult MyAction(int[] deletedItems)
{
// deletedItems contains all values that were checked
// when the submit button was clicked. Here you can
// loop through the array of IDs and delete by ID.
...
}
Run Code Online (Sandbox Code Playgroud)
请注意,复选框没有id
属性.它不用于模型绑定.相反,它有一个name
名为"deletedItems" 的属性,它与MyAction
控制器动作的参数名称相匹配,这就是模型绑定时使用的属性.已value
选中复选框的属性将用于填充deletedItems
数组int[]
.