CheckBox 在 ASP.NET MVC 中总是返回 null

更多 2 知道

单击复选框时,使用 jQuery 提交表单,但复选框始终返回 null。

我有以下剃刀代码:

@using (Html.BeginForm("Index", "HumanResource", new { type = ViewBag.CurrentType }, FormMethod.Post, new { id = "form1" }))
{
 <label class="custom-control custom-checkbox" for="Filter">
        @Html.CheckBox("Filter", new { @class = "custom-control-input" })
        <span class="custom-control-indicator"></span>
        Choose
 </label>
}

// Scripts

<script>
$('#form1 :checkbox').change(function() {
            $("#form1").submit();
   });
</script>

控制器

public async Task<ActionResult> Index(FormCollection fc,int? Filter)
{
    // leftFilter is always null
}
许久

当提交表单时,浏览器将发送truefalse用于filter输入表单数据,到窗体actionURL(其中处理表单提交操作方法)。您正在使用一个可为空的 int( int?) 作为参数来接受 的值filter模型绑定器无法将布尔值映射到可为空的 int。它如何知道在数字参数中为 true /false 分配什么值?它可以将传入的布尔值映射到仅布尔参数。

因此,将您的参数类型更改为bool.

[HttpPost]
public async Task<ActionResult> Index4(FormCollection fc, bool filter)
{
    //to so : return something
}

现在,当提交表单时,filter参数将填充一个值。

建议:当用户单击复选框时提交表单似乎是一种糟糕的用户体验恕我直言,如果用户想要更改怎么办?添加一个单独的“搜索”按钮并在用户选择复选框后单击该按钮时提交表单如何?或者也许考虑一些客户端/ajax 类型的过滤。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ASP.NET MVC 5属性路由:Url.Action返回null

ASP.NET MVC DropDownListFor在回发时返回null

ASP.NET MVC-登录成功,但userId返回null

Google ASP.NET MVC5上的AuthenticationManager.GetExternalLoginInfoAsync()返回null

System.Web.HttpContext.Current.get在ASP.NET MVC控制器中返回null

User.Identity.Name在ASP.NET Core MVC应用程序中返回null

Url.Link总是返回在ASP.NET Web API中使用的Null

Asp.net MVC SQL Server连接返回null

ASP.NET MVC 5 HttpPostedFileBase在模型中始终为null

ASP.NET CORE MVC ViewModel属性在发布时返回Null

ASP.Net MVC控制器构造函数显示错误System.Web.Mvc.Controller.Session.get返回null

在ASP.NET MVC中回发模型为null

在ASP.NET MVC中,模型绑定通用列表为null

如何使用JQuery在MVC asp.net中获取Checkbox值

ASP.NET MVC 5中的Null引用异常

asp.net mvc中的对象数组数据始终为null

使用CheckBox和Integer的ASP.NET MVC的Telerik UI

ASP.NET MVC 5 Html.Checkbox仅在发布时返回默认值

在Asp.Net MVC中,为什么我的代码中ApplicationUser始终为null

ASP.Net MVC ajax总是返回错误(带有路由的问题)

在 asp.net mvc 应用程序中传递 null 而不是字符串的动作

在 asp.net mvc 中返回操作

ASP.NET MVC 5 ModelState.IsValid 在 MVC 中总是返回 false

会话值在 mvc5 asp.net 中返回 null

Ajax调用asp.net mvc 5控制器,参数返回null

ASP.NET MVC 中的发布操作总是收到 null

LINQ 查询返回 NULL ASP-NET-CORE MVC

ASP.NET CheckBox 总是返回假值

JsonConvert.DeseDeserializeObject 在 ASP.NET MVC 中返回 null c#