C# 运行时异常
这是我得到的例外: 参数字典包含“AuthSys.Controllers.MemberController”中方法“System.Web.Mvc.ActionResult AddMemberSystem.String、System.String、System.DateTime、Int32、System.String”的非空类型“System.DateTime”的参数“birthDate”的空条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数AVN:参数 我的观点是这样的: 以及viewmodel 当我尝试运行我的应用程序时会出现错误。通过互联网搜索这个错误,他们都指向路由,这让我更加困惑。我不完全理解错误消息。看起来它说Date是空值,但在视图中,用户将提供Date参数。谁能解释一下我的问题是什么?非常感谢 如果需要,我的路线如下所示:C# 运行时异常,c#,asp.net,asp.net-mvc,visual-studio,C#,Asp.net,Asp.net Mvc,Visual Studio,这是我得到的例外: 参数字典包含“AuthSys.Controllers.MemberController”中方法“System.Web.Mvc.ActionResult AddMemberSystem.String、System.String、System.DateTime、Int32、System.String”的非空类型“System.DateTime”的参数“birthDate”的空条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。 参数AVN:参数 我的观点是这样的:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Member", action = "AddMember", id = UrlParameter.Optional }
);
-----------更新------------
对不起,我不认为视图的完整代码是必需的。这是完整的代码
@model AuthSys.ViewModels.MemberViewModel
@{
ViewBag.Title = "Medlem";
}
<h2>Member</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="imageComponent">
<div class="memberPhoto">
<p>Billed mangler</p>
<br />
</div>
<input type="button" value="Tag et billed" style="float:right; margin-right: 20px;" />
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<br />
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.BirthDate)
@Html.TextBoxFor(model => model.BirthDate, new { size = "50", @type = "date", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.Age)
@Html.TextBoxFor(model => model.Age, new { maxlength = "3", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.SportType)
@Html.TextBoxFor(model => model.SportType, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
<input type="submit" value="Opret" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Registrerede medlemmer", "RegisteredMembers")
</div>
您的代码似乎没有包装在表单中。因此,在提交请求时,不会对参数进行表单绑定 解决方案-将代码包装到表单中。请参阅下面的代码
@using (Html.BeginForm("AddMember", "<<ControllerName>>", new { }, FormMethod.Post))
{
<div class="col-md-offset-2 col-md-10">
<br />
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.BirthDate)
@Html.TextBoxFor(model => model.BirthDate, new { size = "50", @type = "date", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.Age)
@Html.TextBoxFor(model => model.Age, new { maxlength = "3", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.SportType)
@Html.TextBoxFor(model => model.SportType, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
<input type="submit" value="Opret" class="btn btn-default" />
</div>
}
从您调用addmember函数的地方可以看出代码或htmlhat将来自视图。我在topCan上提供的第一个代码请将代码包装在form HTML属性中。您可以使用@Html.begin将完整的Html与表单标记一起共享。在这篇文章中有几个很好的答案。我觉得问题出在视图代码中,一个空值被传递到控制器操作中。您可以通过将操作中的参数设置为null并检查值是否为null来检查这一点。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Member", action = "AddMember", id = UrlParameter.Optional }
);
@model AuthSys.ViewModels.MemberViewModel
@{
ViewBag.Title = "Medlem";
}
<h2>Member</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="imageComponent">
<div class="memberPhoto">
<p>Billed mangler</p>
<br />
</div>
<input type="button" value="Tag et billed" style="float:right; margin-right: 20px;" />
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<br />
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.BirthDate)
@Html.TextBoxFor(model => model.BirthDate, new { size = "50", @type = "date", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.Age)
@Html.TextBoxFor(model => model.Age, new { maxlength = "3", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.SportType)
@Html.TextBoxFor(model => model.SportType, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
<input type="submit" value="Opret" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Registrerede medlemmer", "RegisteredMembers")
</div>
@using (Html.BeginForm("AddMember", "<<ControllerName>>", new { }, FormMethod.Post))
{
<div class="col-md-offset-2 col-md-10">
<br />
@Html.LabelFor(model => model.FirstName)
@Html.TextBoxFor(model => model.FirstName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.LastName)
@Html.TextBoxFor(model => model.LastName, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.BirthDate)
@Html.TextBoxFor(model => model.BirthDate, new { size = "50", @type = "date", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.Age)
@Html.TextBoxFor(model => model.Age, new { maxlength = "3", size = "50", @class = "AddMemberControls" })
<br /><br />
@Html.LabelFor(model => model.SportType)
@Html.TextBoxFor(model => model.SportType, new { maxlength = "50", size = "50", @class = "AddMemberControls" })
<br /><br />
<input type="submit" value="Opret" class="btn btn-default" />
</div>
}