C# 在ASP.NET MVC 5中使用GET方法绑定表单复选框中的参数
我有一个类似下面的例子C# 在ASP.NET MVC 5中使用GET方法绑定表单复选框中的参数,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我有一个类似下面的例子 <form action="/" method="get"> <input type="radio" name="sort" value="date-desc" checked> <input type="radio" name="sort" value="price-desc"> <input type="radio" name="sort" value="price-asc"> <i
<form action="/" method="get">
<input type="radio" name="sort" value="date-desc" checked>
<input type="radio" name="sort" value="price-desc">
<input type="radio" name="sort" value="price-asc">
<input type="radio" name="sort" value="like-desc">
<input type="radio" name="sort" value="like-asc">
<input type="checkbox" name="show-discount">
<input type="checkbox" name="show-new">
<input type="submit" class="btn btn-default" value="Search">
</form>
问题是ShowDiscount
和ShowNew
参数始终为空(未正确绑定)
我认为这个问题是由复选框引起的,它被写为cb name=on
而不是cb name=true
。而且,当复选框未选中时,它也不会写入url
有没有合适的方法
还有一种方法可以将
排序
参数映射到枚举吗?创建一个视图模型
,其中包含与表单输入匹配的属性
public class ViewModel
{
public bool ShowDiscount { get; set; }
// add additional properties
}
然后,在视图的表单中,添加@Html.CheckBoxFor(m=>m.ShowDiscount)
您的控制器
方法现在如下所示:
public ActionResult Index(ViewModel v)
{
// do work here.
}
框架将为您进行绑定。使用视图模型
模型
控制器
public ActionResult Edit()
{
MyModel model = new MyModel();
model.date-desc = "date-desc"; // set default
return View(model)
}
public ActionResult Edit(MyModel model)
{
.... // model is correctly bound with selected values
看法
视图(包括@using,用于已定义枚举的程序集)
使用视图模型和强类型html帮助程序,让MVC为您完成这一切。感谢您的回复,请使用您建议的复选框上的value=“true”
,现在一切正常。第二个问题,是否可以将单选按钮Sort
与enum而不是字符串绑定,但保留值date desc、price desc、price asc
?是的,可以使用enum。我将很快更新答案(但请养成使用视图模型和强类型html帮助程序的习惯-你永远不会有这些问题)(但请养成使用视图模型和强类型html帮助程序的习惯-你永远不会有这些问题)
我使用视图模型绑定很好,但这是一个特例,说来话长:)等待更新:)好的,我已经使用视图模型更新了答案。如果您不想总是使用enum.TryParse()
将字符串转换为enum,谢谢您的回答,我很了解视图模型绑定,但我不喜欢使用默认属性名的绑定,我的操作将从外部网站调用,它应该能够通过get方法从表单调用。这是关于眼睛糖果的事情。
@model YourNamespace.ViewModel
public ActionResult Index(ViewModel v)
{
// do work here.
}
public class MyModel
{
public string sort { get; set; }
public bool show-discount { get; set; }
public bool show-discount { get; set; } // or bool? if you want it to be nullable
public bool show-new { get; set; }
}
public ActionResult Edit()
{
MyModel model = new MyModel();
model.date-desc = "date-desc"; // set default
return View(model)
}
public ActionResult Edit(MyModel model)
{
.... // model is correctly bound with selected values
@model MyModel
@using (Html.BeginForm())
{
.....
@Html.RadioButtonFor(m => m.sort, "date-desc", new { id = "date-desc" });
@Html.RadioButtonFor(m => m.sort, "price-desc", new { id = "price-desc" });
@Html.RadioButtonFor(m => m.sort, "price-asc", new { id = "price-asc" });
@Html.RadioButtonFor(m => m.sort, "like-desc", new { id = "like-desc" });
@Html.RadioButtonFor(m => m.sort, "like-asc", new { id = "like-asc" });
@Html.CheckBoxFor(m => m.show-discount);
@Html.CheckBoxFor(m => m.show-new);
<input type="submit" class="btn btn-default" value="Search">
}
public enum MyEnum
{
date-desc,
price-desc,
....
}
public class MyModel
{
public MyEnum sort { get; set; }
....
@Html.RadioButtonFor(m => m.sort, MyEnum.date-desc, new { id = "date-desc" });
@Html.RadioButtonFor(m => m.sort, MyEnum.price-desc, new { id = "date-desc" });
....