1

I am new to MVC and need some guidance for the problem that I am running into.

I have a text field to contain Date using date picker. In the model, I didn't specify to validate the text field.

But I submit the form, the jQuery unobstrusive validates the date text field.

The model

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. ComponentModel. DataAnnotations;
Using System. Linq;
Using System. Web;
Using DataAnnotationsExtensions;


Namespace heavy_haulage_general_freight. Models
{

    public class nsc_gf_job
    {
        [Key]
        public int id { get; set; }
        public DateTime pickup_date { get; set; }
    }
}

The View

@model heavy_haulage_general_freight. Models. Nsc_gf_job    
@using heavy_haulage_general_freight. Helpers

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html. BeginForm())
{
    @Html.ValidationSummary(true)

<fieldset>
    <legend>Resources Division Heavy Haulage Details:</legend>
        <table>
            <tbody>
                <tr>
                    <td>@Html.Label("Pickup Date")</td>
                    <td>
                        @Html.TextBox("pickup_date", "") <br />
                        @Html.ValidationMessage("pickup_date")
                    </td>                
                <tr>
            </tbody>
        </table>
    </fieldset>


        <p>
            <input type="submit" value="Create" />
        </p>

}

2 Answers 2

3

Try using a nullable date - ie

 public DateTime? pickup_date { get; set; }

also note this applies to ints as well, as there is no 'empty' default for an int, you need a nullable value

Sign up to request clarification or add additional context in comments.

Comments

2

That happens because you have defined the property of type DateTime in your model. Validation will always happen for this type because you cannot assign an invalid value to such field.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.