ASP.NET MVC - 复选框列表未返回所有逗号分隔值
ASP.NET MVC - Checkbox list not returning all comma separated values
我有以下复选框列表:
<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");
}
}
选中两个复选框只保存一个值?
您不能直接向服务器获取逗号分隔值,我建议更改 class 如下
public class MyModel
{
public List<string> Categories { get; set; }
}
您将获得选中复选框的值列表。
如果您想要逗号分隔值,那么在客户端需要在提交表单时创建函数并需要保存隐藏变量。
希望对您有所帮助
谢谢
主要问题是 string
属性 存储单个字符串,您应该使用集合 属性 来绑定复选框,例如List<string>
正如@Ajay 之前提到的。
因此,您应该使用此设置:
型号
public class MyModel
{
public MyModel()
{
SelectedCategories = new List<string>();
// put categories here
Categories = new List<SelectListItem>() { ... };
}
// other properties
public List<string> SelectedCategories { get; set; }
public List<SelectListItem> Categories { get; set; }
}
查看
@foreach (var item in Model.Categories)
{
<input type="checkbox" name="SelectedCategories" value="@item.Value" ... />
}
控制器
[HttpPost]
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{
// create comma-separated values
var selectedCategories = string.Join(",", model.SelectedCategories);
// Save data to database, and redirect to Success page.
return RedirectToAction("Success");
}
}
如果你想使用 CheckBoxFor
助手,请使用 SelectListItem
,它有 Selected
属性 和 bool
类型,因为 CheckBoxFor
绑定对于 bool
属性:
型号
public class MyModel
{
public MyModel()
{
// put categories here
Categories = new List<SelectListItem>() { ... };
}
// other properties
public List<SelectListItem> Categories { get; set; }
}
查看
@for (var i = 0; i < Model.Categories.Count; i++)
{
@Html.CheckBoxFor(model => model.Categories[i].Selected)
@Html.HiddenFor(model => model.Categories[i].Text)
@Html.HiddenFor(model => model.Categories[i].Value)
}
控制器
[HttpPost]
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{
string selectedCategories = string.Join(",",
model.Categories.Where(x => x.Selected == true)
.Select(x => x.Text).ToList());
// Save data to database, and redirect to Success page.
return RedirectToAction("Success");
}
}
注:
有一个名为 CheckBoxListFor
的自定义助手,应该考虑从 List<T>
属性.
创建复选框列表
可以看到复选框列表实现的示例here。
相关问题:
Get Multiple Selected checkboxes in 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");
}
}
选中两个复选框只保存一个值?
您不能直接向服务器获取逗号分隔值,我建议更改 class 如下
public class MyModel
{
public List<string> Categories { get; set; }
}
您将获得选中复选框的值列表。
如果您想要逗号分隔值,那么在客户端需要在提交表单时创建函数并需要保存隐藏变量。
希望对您有所帮助
谢谢
主要问题是 string
属性 存储单个字符串,您应该使用集合 属性 来绑定复选框,例如List<string>
正如@Ajay 之前提到的。
因此,您应该使用此设置:
型号
public class MyModel
{
public MyModel()
{
SelectedCategories = new List<string>();
// put categories here
Categories = new List<SelectListItem>() { ... };
}
// other properties
public List<string> SelectedCategories { get; set; }
public List<SelectListItem> Categories { get; set; }
}
查看
@foreach (var item in Model.Categories)
{
<input type="checkbox" name="SelectedCategories" value="@item.Value" ... />
}
控制器
[HttpPost]
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{
// create comma-separated values
var selectedCategories = string.Join(",", model.SelectedCategories);
// Save data to database, and redirect to Success page.
return RedirectToAction("Success");
}
}
如果你想使用 CheckBoxFor
助手,请使用 SelectListItem
,它有 Selected
属性 和 bool
类型,因为 CheckBoxFor
绑定对于 bool
属性:
型号
public class MyModel
{
public MyModel()
{
// put categories here
Categories = new List<SelectListItem>() { ... };
}
// other properties
public List<SelectListItem> Categories { get; set; }
}
查看
@for (var i = 0; i < Model.Categories.Count; i++)
{
@Html.CheckBoxFor(model => model.Categories[i].Selected)
@Html.HiddenFor(model => model.Categories[i].Text)
@Html.HiddenFor(model => model.Categories[i].Value)
}
控制器
[HttpPost]
public ActionResult Index(MyModel model)
{
if (ModelState.IsValid)
{
string selectedCategories = string.Join(",",
model.Categories.Where(x => x.Selected == true)
.Select(x => x.Text).ToList());
// Save data to database, and redirect to Success page.
return RedirectToAction("Success");
}
}
注:
有一个名为 CheckBoxListFor
的自定义助手,应该考虑从 List<T>
属性.
可以看到复选框列表实现的示例here。
相关问题:
Get Multiple Selected checkboxes in MVC