在ASP.NET MVC 5中,使用GET方法绑定表单的复选框中的参数

本文关键字:绑定 方法 参数 表单 GET 复选框 使用 NET ASP MVC | 更新日期: 2023-09-27 18:24:49

我有一个类似的

<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>

它返回的url类似于:<site>?sort=date-desc&show-discount=on&show-new=on

我想将其绑定到控制器操作,操作如下:

public ActionResult Index([Bind(Prefix = "sort")]string Sort, 
    [Bind(Prefix = "show-discount")]bool? ShowDiscount = null, 
    [Bind(Prefix = "show-new")]bool? ShowNew = null)
{
}

问题是CCD_ 2和CCD_。

我认为复选框在选中时导致的问题将其写为cb-name=on而不是cb-name=true。并且当复选框未选中时,它将不会写在url上。

有合适的方法吗?

有没有办法将Sort参数映射到枚举?

在ASP.NET MVC 5中,使用GET方法绑定表单的复选框中的参数

使用视图模型

型号

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">
}

注意,您可以制作sort和enum。如果布尔值可以为null,CheckBoxFor将呈现一个包含3个值的下拉列表(允许选择null)

请注意,在您的代码中,您不需要[Bind(Prefix..。您本可以使用<input type="checkbox" name="show-discount" value="true">,但这只适用于不可为null的布尔值,因为未选中的复选框不会返回。如果你不检查它,你会得到null,而不是false

编辑

如果你想使用枚举

public enum MyEnum
{
  date-desc,
  price-desc,
  ....
}
public class MyModel
{
  public MyEnum sort { get; set; }
  ....

视图(包括用于定义枚举的程序集的@using)

@Html.RadioButtonFor(m => m.sort, MyEnum.date-desc, new { id = "date-desc" });
@Html.RadioButtonFor(m => m.sort, MyEnum.price-desc, new { id = "date-desc" });
....

创建一个包含与表单输入匹配的属性的ViewModel

public class ViewModel 
{
    public bool ShowDiscount { get; set; }
    // add additional properties
}

然后,在view上的form中,添加@Html.CheckBoxFor(m => m.ShowDiscount)

<form action="/" method="get">
    @Html.CheckBoxFor(m => m.ShowDiscount)
    <input type="submit" class="btn btn-default" value="Search">
</form>

请确保让view知道它应该期待哪种类型的model。在view的顶部指定。

@model YourNamespace.ViewModel

你的ShowDiscount0方法现在看起来是这样的:

public ActionResult Index(ViewModel v)
{
    // do work here.
}

该框架将为您进行绑定。