运行时的ArgumentException

时间:2018-04-30 16:24:01

标签: c# asp.net asp.net-mvc visual-studio

这是我得到的例外:

参数字典包含方法'System.Web.Mvc.ActionResult AddMember(System.String,System.String,System)的非可空类型'System.DateTime'的参数'birthDate'的空条目。 'AuthSys.Controllers.MemberController'中的DateTime,Int32,System.String)'。可选参数必须是引用类型,可空类型,或者声明为可选参数。 Parameternavn:参数

我的观点如下:

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

我的控制器:

public ActionResult AddMember(string fName, string lName, DateTime birthDate, int age, string sportType)
{
    var member = new Member() { FirstName = fName, LastName = lName, Age = age, SportType = sportType, BirthDate = birthDate};
    coloContext.Members.Add(member);
    coloContext.SaveChanges();         
    return View(member);
}

和viewmodel

public class MemberViewModel
{
    public string FirstName { get; set; }       
    public string LastName { get; set; }        
    public DateTime BirthDate { get; set; }                       
    public DateTime CreationDate { get; set; }        
    public int Age { get; set; }        
    public string SportType { get; set; }
}

当我尝试运行我的应用程序时出现错误。通过互联网搜索这个错误,他们都指向路由,这让我更加困惑。我不完全理解错误消息。看起来它说Date是一个空值,但在视图中,用户将提供date参数。谁能解释一下我的问题是什么?非常感谢。

如果需要,我的路由如下:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Member", action = "AddMember", id = UrlParameter.Optional }
);

---------------------更新------------------------ < / p>

抱歉,我认为该视图的完整代码不是必需的。这是完整的代码

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

1 个答案:

答案 0 :(得分:1)

似乎您的代码未包含在表单中。因此,在提交请求时,参数不会发生表单绑定。

解决方案 - 将代码包装在Form中。请参阅以下代码。

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