0

This is the exception I get:

The parameters dictionary contains a null entry for parameter 'birthDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult AddMember(System.String, System.String, System.DateTime, Int32, System.String)' in 'AuthSys.Controllers.MemberController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameternavn: parameters

My view looks like this:

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

My contoller:

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);
}

And the 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; }
}

The error comes when I try to run my application. Searching on this error over the internet, they all point to the routing, which confuses me even more. I do not fully understand the error message. It looks like it says that Date is a null value, but in the view, the user will be providing the date parameter. Can anyone please explain what my problem is? Thanks a lot.

In case needed, my routing looks like this:

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

---------------------Update------------------------

Sorry, I did not think the full code of the view was necassary. Here is the full code

    @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>
MOR_SNOW
  • 787
  • 1
  • 5
  • 16

1 Answers1

1

It seems that your code is not wrapped inside the form. So while submitting a request, the form binding is not happening for parameters.

Solution - Wrap the code inside Form. See the below code.

@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> 
}
user1672994
  • 10,509
  • 1
  • 19
  • 32