post 返回 MVC 中的复选框值
post back the checkbox value in MVC
我有一个模型如下
public class security
{
public long id { get; set; }
public long user_id { get; set; }
public long submenu_id { get; set; }
public long module_id { get; set; }
public long flag { get; set; }
public string module { get; set; }
public string submenu { get; set; }
}
其中 flag
将是 1
如果用户可以访问该菜单,否则 0
我可以使用分页列表在视图中显示它
@foreach (var item in Model)
{
<tr class="">
<td>
@if (item.flag == 1)
{
<input type="checkbox" name="flags[]" checked="checked" id="@item.id" value="1" />
}
else
{
<input type="checkbox" name="flags[]" id="@item.id" value="0" />
}
</td>
<td>@Html.DisplayFor(modelItem => item.module) </td>
<td> @Html.DisplayFor(modelItem => item.submenu)</td>
</tr>
}
它将给出以下输出
此处用户有权访问 5
菜单,但无权访问 1
现在管理员可以通过选中和取消选中复选框来更改权限。
我的post actionresult签名如下
public ActionResult security(client_module_security c1,string[] flags)
{
}
但这不是 post返回此处的值,并且标志字符串仅包含选中的数字。下面的屏幕截图
如果我的逻辑不好,请指正。要更新相同内容需要进行哪些更改?
型号:
public class security
{
public long id { get; set; }
public long user_id { get; set; }
public long submenu_id { get; set; }
public long module_id { get; set; }
public bool flag { get; set; }
public string module { get; set; }
public string submenu { get; set; }
}
查看:
@for(var i = 0; i<Model.Count; i++)
{
@Html.HiddenFor(m=>m[i].id)
<tr class="">
<td>
@Html.CheckBoxFor(m=>m[i].flag)
</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].module) </td>
<td> @Html.DisplayFor(modelItem => modelItem[i].submenu)</td>
</tr>
}
控制器
public ActionResult security(IList<security> model)
{
//each one of the items in your model will have Id an flag property filled with data
}
所有复选框的值都是 0
或 1
,因为您使用 <input .. value="1" />
或 <input .. value="0" />
设置它们(并且因为只有选中的复选框 post 回来,你在控制器中的值总是 1
)。相反,将 value
属性设置为项目
的 id
<input type="checkbox" name="flags[]" checked="checked" value ="@item.id" />
string[] flags
参数现在将包含所有选中项目的 ID 数组。
我有一个模型如下
public class security
{
public long id { get; set; }
public long user_id { get; set; }
public long submenu_id { get; set; }
public long module_id { get; set; }
public long flag { get; set; }
public string module { get; set; }
public string submenu { get; set; }
}
其中 flag
将是 1
如果用户可以访问该菜单,否则 0
我可以使用分页列表在视图中显示它
@foreach (var item in Model)
{
<tr class="">
<td>
@if (item.flag == 1)
{
<input type="checkbox" name="flags[]" checked="checked" id="@item.id" value="1" />
}
else
{
<input type="checkbox" name="flags[]" id="@item.id" value="0" />
}
</td>
<td>@Html.DisplayFor(modelItem => item.module) </td>
<td> @Html.DisplayFor(modelItem => item.submenu)</td>
</tr>
}
它将给出以下输出
此处用户有权访问 5
菜单,但无权访问 1
现在管理员可以通过选中和取消选中复选框来更改权限。
我的post actionresult签名如下
public ActionResult security(client_module_security c1,string[] flags)
{
}
但这不是 post返回此处的值,并且标志字符串仅包含选中的数字。下面的屏幕截图
如果我的逻辑不好,请指正。要更新相同内容需要进行哪些更改?
型号:
public class security
{
public long id { get; set; }
public long user_id { get; set; }
public long submenu_id { get; set; }
public long module_id { get; set; }
public bool flag { get; set; }
public string module { get; set; }
public string submenu { get; set; }
}
查看:
@for(var i = 0; i<Model.Count; i++)
{
@Html.HiddenFor(m=>m[i].id)
<tr class="">
<td>
@Html.CheckBoxFor(m=>m[i].flag)
</td>
<td>@Html.DisplayFor(modelItem => modelItem[i].module) </td>
<td> @Html.DisplayFor(modelItem => modelItem[i].submenu)</td>
</tr>
}
控制器
public ActionResult security(IList<security> model)
{
//each one of the items in your model will have Id an flag property filled with data
}
所有复选框的值都是 0
或 1
,因为您使用 <input .. value="1" />
或 <input .. value="0" />
设置它们(并且因为只有选中的复选框 post 回来,你在控制器中的值总是 1
)。相反,将 value
属性设置为项目
id
<input type="checkbox" name="flags[]" checked="checked" value ="@item.id" />
string[] flags
参数现在将包含所有选中项目的 ID 数组。