C# 远程验证随机抛出;字典包含一个空条目;错误
当远程验证尝试检查数据库中是否已经存在值时,我遇到了这个非常恼人的错误: 参数字典包含“ADVWKSP.Controllers.CRMTItemsController”中方法“System.Web.Mvc.JsonResult doesgbsNumberList(Int32)”的不可为null类型“System.Int32”的参数“gbsNumber”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 视图模型C# 远程验证随机抛出;字典包含一个空条目;错误,c#,ajax,asp.net-mvc,remote-validation,C#,Ajax,Asp.net Mvc,Remote Validation,当远程验证尝试检查数据库中是否已经存在值时,我遇到了这个非常恼人的错误: 参数字典包含“ADVWKSP.Controllers.CRMTItemsController”中方法“System.Web.Mvc.JsonResult doesgbsNumberList(Int32)”的不可为null类型“System.Int32”的参数“gbsNumber”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数名称:参数 视图模型 public class CRMTItem
public class CRMTItemViewModel
{
public int Id { get; set; }
[Required]
[Display(Name = "Project Title")]
[Remote("DoesProjectTitleExist", "CRMTItems", HttpMethod = "POST",
ErrorMessage = "Workspace for that project title already exists.")]
public string ProjectTitle { get; set; }
[Required]
[Display(Name = "Project Stage")]
public ProjectStage? ProjectStage { get; set; }
[Required]
[Display(Name = "CRMT Number")]
[Remote("DoesCrmtNumberExist", "CRMTItems", HttpMethod = "POST",
ErrorMessage = "Workspace for that CRMT number already exists.")]
public int? CRMTNumber { get; set; }
[Required]
[Display(Name = "GBS Number")]
[Remote("DoesGbSNumberExist", "CRMTItems", HttpMethod = "POST",
ErrorMessage = "Workspace for that GBS project number already exists.")]
public int? GbSNumber { get; set; }
public bool Confidential { get; set; }
[Required]
[Display(Name = "Project Managers")]
public IEnumerable<string> SelectedTags { get; set; }
}
公共类CRMTItemViewModel
{
公共int Id{get;set;}
[必需]
[显示(Name=“项目标题”)]
[远程(“DoesProjectTitleExist”、“CRMTItems”,HttpMethod=“POST”,
ErrorMessage=“该项目标题的工作区已存在。”)]
公共字符串ProjectTitle{get;set;}
[必需]
[显示(Name=“项目阶段”)]
公共ProjectStage?ProjectStage{get;set;}
[必需]
[显示(Name=“CRMT编号”)]
[远程(“DoesCrmtNumberExist”、“CRMTItems”、HttpMethod=“POST”,
ErrorMessage=“该CRMT编号的工作区已存在。”)]
公共int?CRMTNumber{get;set;}
[必需]
[显示(Name=“GBS编号”)]
[远程(“DoesGbSNumberExist”、“CRMTItems”、HttpMethod=“POST”,
ErrorMessage=“该GBS项目编号的工作区已存在。”)]
公共int?GbSNumber{get;set;}
公共布尔机密{get;set;}
[必需]
[显示(Name=“项目经理”)]
公共IEnumerable SelectedTags{get;set;}
}
查看
@using (Ajax.BeginForm("Create", "CRMTItems", new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = "divToUpdate",
OnBegin = "submitBegin",
OnSuccess = "submitSuccess",
OnFailure = "submitFailure",
OnComplete = "submitComplete"
}, new { @ID = "AjaxForm" }))
{
<div class="form-horizontal">
<h4>New Project Workspace Form</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProjectTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProjectTitle, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.ProjectTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectStage, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EnumDropDownListFor(model => model.ProjectStage, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.ProjectStage, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CRMTNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CRMTNumber, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.CRMTNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.GbSNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.GbSNumber, new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.GbSNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Confidential, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.CheckBoxFor(model => model.Confidential, new { @class = "form-control", @style = "height:17px;" })
@Html.ValidationMessageFor(model => model.Confidential, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.SelectedTags, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(m => m.Id)
@Html.ListBoxFor(m => m.SelectedTags, new SelectList(users, "UserName", "DisplayName"), new { @class = "teamSelecter", name = "states[]", multiple = "multiple", style = "display:none; width:100%;"})
@Html.ValidationMessageFor(model => model.SelectedTags, "", new { @class = "text-danger" })
<p id="pmWarning" class="text-danger" hidden>Please select one or more project managers</p>
</div>
</div>
<br />
<button id="formSubmit" class="btn btn-default btn-lg pull-right" type="submit" value="submit">Submit</button>
<div class="loader pull-right" hidden></div>
</div>
}
@使用(Ajax.BeginForm(“创建”、“CRMTItems”、新的AjaxOptions
{
HttpMethod=“Post”,
UpdateTargetId=“divToUpdate”,
OnBegin=“submitBegin”,
OnSuccess=“submitSuccess”,
OnFailure=“submitFailure”,
OnComplete=“submitComplete”
},新的{@ID=“AjaxForm”})
{
新项目工作区表单
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectTitle,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.ProjectTitle,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProjectTitle,“,new{@class=“text danger”})
@LabelFor(model=>model.ProjectStage,htmlAttributes:new{@class=“controllabel col-md-2”})
@EnumDropDownListFor(model=>model.ProjectStage,新的{@class=“form control”})
@Html.ValidationMessageFor(model=>model.ProjectStage,“,new{@class=“text danger”})
@LabelFor(model=>model.CRMTNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CRMTNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.CRMTNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.GbSNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.GbSNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.GbSNumber,“,new{@class=“text danger”})
@LabelFor(model=>model.Confidential,htmlAttributes:new{@class=“controllabel col-md-2”})
@CheckBoxFor(model=>model.Confidential,新的{@class=“formcontrol”,@style=“height:17px;”})
@Html.ValidationMessageFor(model=>model.Confidential,“,new{@class=“text danger”})
@LabelFor(model=>model.SelectedTags,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.HiddenFor(m=>m.Id)
@Html.ListBoxFor(m=>m.SelectedTags,new SelectList(用户,“用户名”,“显示名称”),new{@class=“teamselector”,name=“states[]”,multiple=“multiple”,style=“显示:无;宽度:100%;”)
@Html.ValidationMessageFor(model=>model.SelectedTags,“,new{@class=“text danger”})
请选择一个或多个项目经理
提交
}
我将参数类型更改为int?
,从那以后就再也没有看到错误,因此希望这一问题得到解决,但我们会看到的
更新控制器:
[HttpPost]
public JsonResult DoesGbSNumberExist(int? gbsNumber)
{
using (var db = new ADVWKSPEntities())
{
return (gbsNumber == null) ? Json(false) : Json(!db.CRMTItems.Any(i => i.GbsNumber == gbsNumber));
}
}
视图模型将整数值定义为可为null的int
int?
。可为空的int只有在包含值时才能与int进行比较
您的线路:
return Json(!db.CRMTItems.Any(i => i.GbsNumber == gbsNumber));
应测试空值:
return Json(!db.CRMTItems.Any(i => (i.GbsNumber ?? 0) == gbsNumber));
用GbsNumber不包含的值替换零
return Json(!db.CRMTItems.Any(i => i.GbsNumber == gbsNumber));
return Json(!db.CRMTItems.Any(i => (i.GbsNumber ?? 0) == gbsNumber));