Question

I have an HttpPost controller action that takes in a simple form DTO object.

[HttpPost]
public ViewResult Index(ResultQueryForm queryForm)
{
   ...
}

public class ResultQueryForm
{
   public DateTime? TimestampStart { get; set; }
   public DateTime? TimestampEnd { get; set; }
   public string Name { get; set; }
}

The DTO object has nullable datetime fields used to create a range. The reason that it is set to nullable is because the form that is bound to the model is a query form, and the user doesn't have to enter a date value in the form.

The problem I'm running into is that if the user enters an invalid date, i would like the MVC default model binding to provide an error message. This happens flawlessly if I have a controller action that takes a DateTime? type as a argument, but since I'm passing a DTO that holds a DateTime? type the model binding appears to just set the DateTime? variable to null. This causes unexpected results.

Note:

[HttpPost]
public ViewResult Index(DateTime? startDate)
{
   // If the user enters an invalid date, the controller action won't even be run because   the MVC model binding will fail and return an error message to the user
}

Is there anyway to tell MVC model binding to "fail" if it can't bind the DateTime? value to the form DTO object, instead of just setting it to null? Is there a better way? Passing each individual form input to the controller is infeasible, due to the large amount of properties in the form/dto object (I've excluded many of them for easy reading).

Was it helpful?

Solution

You can validate your model in the controller action.

if(!Model.IsValid)
{
  return View(); // ooops didn't work
}
else
{
  return RedirectToAction("Index"); //horray
}

Of course you can put whatever you want in there, and return Json object if you want to display it on your page.

Also you need to add ValidateInput(true) up the top of your action method like this: [HttpPost, ValidateInput(true)]

OTHER TIPS

I think you can create a custom ValidationAttribute for this.

[DateTimeFormat(ErrorMessage = "Invalid date format.")]
public DateTime? TimestampStart { get; set; }
[DateTimeFormat(ErrorMessage = "Invalid date format.")]
public DateTime? TimestampEnd { get; set; }


[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DateTimeFormatAttribute : ValidationAttribute
{
    public override bool IsValid(object value) {

        // allow null values
        if (value == null) { return true; }

        // when value is not null, try to convert to a DateTime
        DateTime asDateTime;
        if (DateTime.TryParse(value.ToString(), out asDateTime)) {
            return true; // parsed to datetime successfully
        }
        return false; // value could not be parsed
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top