asp.net-mvc - ASP.NET MVC - 复选框列表未返回所有逗号分隔值
问题描述
我有以下复选框列表:
<input type="checkbox" name="Categories" value="1" />
<input type="checkbox" name="Categories" value="2" />
我的模型如下:
public class MyModel
{
public string Categories { get; set; }
}
我的控制器:
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{
// Save data to database, and redirect to Success page.
return RedirectToAction("Success");
}
}
选择两个复选框只保存一个值?
解决方案
您不能直接将逗号分隔值获取到服务器,我建议更改类如下
public class MyModel
{
public List<string> Categories { get; set; }
}
您将获得选中复选框的值列表。
如果您想要逗号分隔值,那么在客户端需要提交表单创建功能并需要保存隐藏变量。
愿这对你有帮助
谢谢
推荐阅读
- javascript - 运行 yarn install 时出现 ENOTFOUND registry.yarnpkg.com 错误
- kdb - 访问 kdb+/Q 中的子文件夹
- android - 应用程序版本已在 Play 商店更新,但商店信息未更新
- git - 将 Git 提交合并到具有不同目录的另一个存储库中
- html - 如何将映射图像上传为 LinkedIn 帖子?
- devops - Digital Ocean 中的外部体积监测
- java - 503 服务不可用 - 我的 ingress.yaml 文件有问题吗?
- vb.net - 将图像拉伸到十六进制图片框
- react-native - React Native - 从水平选择器中获取价值
- java - 如何在 PDFBox 中将字符串保存为 PDF?