0

I have notice something special in asp.net mvc.

I did not specific any validation for DateTime. However, when i press the create button, it will auto display the error message if it is left empty or the format is not compatible with DateTime format. Is this predefined in ASP.NET MVC?

AFAIK, it is not available in ASP.NET MVC3? I am now currently using MVC 5. Which version is it available?

Secondly, I would like to know what is the use of ModelState.IsValid in there and what is the use of it?

This is because i notice that the model state is always valid, if it not valid, the create post method would not be invoked as the error message will be displayed beforehand.

public class Employee
{
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
    public DateTime DateOfBirth { get; set; }
}

[HttpPost]
[ActionName("Create")]
public ActionResult Create_Post()
{
    Employee employee = new Employee();
    UpdateModel(employee);

    if (ModelState.IsValid)
    {
        EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer();
        employeeBusinessLayer.AddEmployee(employee);

        return RedirectToAction("Index");
    }

    return View();
}
2
  • Share the ASP code for the date time selector. Are you using HTML 5? Commented Sep 22, 2014 at 16:02
  • <div class="form-group"> Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { class = "control-label col-md-2" }) <div class="col-md-10"> Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } }) Html.ValidationMessageFor(model => model.DateOfBirth, "", new { class = "text-danger" }) </div> </div> Commented Sep 22, 2014 at 16:05

3 Answers 3

0

Jack M's answer addresses your first question about the Date being validated because it is non-nullable in your model.

To address your second question, ModelState.IsValid exists to ensure that server-side validation is run before you process the action. MVC automatically adds some client-side (Javascript) validation into the HTML, which ensures that you've filled in the value of the field before the HTTP post, for example. However, if you were to disable Javascript, or use more complex validators, these would be caught by the server-side validation instead - this is where ModelState.IsValid is used.

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

Comments

0

you are getting an error because you datetime is not nullable, and the isValid will check that the model is valid based on validation setup in your case it will be invalid when the name isn't present along any non nullable objects

Comments

0

Please check the below link. Hope this will help your question.

Change message default @Html.ValidationMessageFor mvc 4

As you have included the Html.ValidationMessageFor method for DateOfBirth, It will give the default error message. You can further edit the error message based on condition. That can be done using the attribute for the property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.