Coming from winforms, I am quite new to MVC and learning. So pls bear with me. I am attempting to do some validations the right way in MVC. I have some code which does using JS, but I would like to modify that to validate using MVC.
This is my Model
public class MyViewModel
{
/// <summary>
/// Start Date
/// </summary>
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}",ApplyFormatInEditMode=true)]
[Display(ResourceType = typeof(UIResources.GeneralPurpose), Name = "StartDate")]
public DateTime StartDate { get; set; }
/// <summary>
/// End Date
/// </summary>
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[Display(ResourceType = typeof(UIResources.GeneralPurpose), Name = "EndDate")]
public DateTime EndDate { get; set; }
}
This is how my html looks
@model MyViewModel
@using ...
{
<div id="ErrMsg" class="validation-summary-errors center">
</div>
using (Ajax.BeginForm("someAction", "someController", new AjaxOptions()))
{
@Html.ValidationSummary()
<fieldset class="control-set-container no-border">
<ul class="undecorated-list inline-list-items">
<li>
@Html.LabelFor(model => model.StartDate)
@Html.MyDateTimePicker(model => model.StartDate)
</li>
<li>
@Html.LabelFor(model => model.EndDate)
@Html.MyDateTimePicker(model => model.EndDate)
</li>
</ul>
</fieldset>
}
@Html.MyDateTimePicker writes some HTML that gives out a text box and a datepicker control (Text box used to display date in some date format).
Here is what I am trying to validate,
- If date is not in correct format (dd/MM/yyyy), the errMsg.Html should be set to the message specified in the view model.
- If the startDate is greater than endDate, then display message in errMsg.Html.
- While I am at it, I would like to is verify if the date is in one of 2 different formats (dd/MM/yyyy or M/d/yyyy), display the error message when validation fails.