![](/img/trans.png)
[英]The parameters dictionary contains a null entry for parameter of non-nullable type 'System.Int32'
[英]“The parameters dictionary contains a null entry for parameter 'blueColor' of non-nullable type 'System.Boolean'” - How to fix this?
我对 MVC 应用程序很陌生,并且在组合 html 帮助程序和控制器时遇到了问题。 我希望我能很好地暴露我的问题,所以这里是:
所以我有这些复选框:
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm())
{
<p>
<label>
Obj Colors
</label>
Blue : @Html.CheckBox("blueColor", true)
Red : @Html.CheckBox("redColor", true)
Green : @Html.CheckBox("greenColor", true) <br/>
Black : @Html.CheckBox("blackColor", true)
White : @Html.CheckBox("whiteColor", true)
</p>
(...)
}
我的控制器中有这个巨大的方法:
public ActionResult SearchIndex(string objName, string objType, string objSymbol, string objValue, string artistName, bool blueColor, bool redColor, bool greenColor, bool blackColor, bool whiteColor, bool colorless)
{
(...)
if (blueColor || redColor || greenColor || blackColor || whiteColor || colorless)
{
(...)
}
(...)
}
但是当我加载 SearchIndex 所在的页面时,因为我已经引入了这些布尔值,所以我一进入页面就会一直在标题中提到崩溃。 什么地方出了错? 请帮我。
错误说明了一切,真的。 布尔值不能为空,显然 blueColor 是。
您可以更改方法签名以接受Nullable<bool>
像这样
public ActionResult SearchIndex(string objName, string objType, string objSymbol, string objValue, string artistName, bool? blueColor, bool? redColor, bool? greenColor, bool? blackColor, bool whiteColor, bool colorless)
{
或者,如果您希望将值从视图传递到方法中,请将您发布的那些帮助程序包装在表单中并使您的控制器操作接受发布请求
@using (Html.BeginForm( ))
{
Blue : @Html.CheckBox("blueColor", true)
Red : @Html.CheckBox("redColor", true)
Green : @Html.CheckBox("greenColor", true) <br/>
Black : @Html.CheckBox("blackColor", true)
White : @Html.CheckBox("whiteColor", true)
}
[HttpPost]
public ActionResult SearchIndex(string objName, string objType, string objSymbol, string objValue, string artistName, bool blueColor, bool redColor, bool greenColor, bool blackColor, bool whiteColor, bool colorless)
{
编辑
或者,如果您试图以 get 方式执行此操作,出于某种原因,您必须在表单声明中指定:
@using (Html.BeginForm("SearchIndex", "ControllerName", FormMethod.Get ))
{
但是(如果您没有将 bool 参数设为 Nullable),您必须将值传递给此方法才能加载页面
您的 ActionLink 必须将一些值传递给不可为空的 bool 的操作,如下所示:
@Html.ActionLink("Cards", "SearchIndex", "Card", new {blueColor = true, redColor = false}, null)
使用 true 或 false 在您的 API 签名中初始化 bool
public ActionResult SearchIndex(string objName, string objType, bool blueColor =false, bool redColor=true )
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.