After completing several tutorials, I have successfully implemented my models from a library file (dll). Everything seems to be functioning correctly except for one issue.
Here is my model:
public class RoomBookingInsert
{
public Int32 CostCentreNo { get; set; }
public Int32 CustomerAccNo { get; set; }
public Int32 RoomNo { get; set; }
public Int32 ServiceCode { get; set; }
[PriceValidation]
public Decimal HourlyRate { get; set; }
[DataType(DataType.Date)]
[DateRange("2010/12/01", "2010/12/16")]
public DateTime StartDate { get; set; }
}
Although the attributes are recognized and highlighted accordingly, when submitting the form it accepts any input.
I have included the code below for validation attributes, understanding that this validation occurs on the server side upon form submission.
This is my form in asp.net mvc3 using razor:
@model MyLibrary.RoomBookingInsert
@{
ViewBag.Title = "Temp";
}
<h2>Temp</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>RoomBookingInsert</legend>
@Html.EditorForModel()
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Firstly, here is the Price validation attribute (ensuring no negative values).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
public class PriceValidationAttribute : ValidationAttribute
{
private decimal minPrice = 0.00M;
private decimal maxPrice = 100.00M;
public PriceValidationAttribute()
{
}
public override bool IsValid(object value)
{
decimal price = (decimal)value;
if (price < this.minPrice || price > this.maxPrice)
return false;
return true;
}
}
Next, we have the Date Range validation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
public class DateRangeAttribute : ValidationAttribute
{
private const string DateFormat = "yyyy/MM/dd";
private const string DefaultErrorMessage =
"'{0}' must be a date between {1:d} and {2:d}.";
public DateTime MinDate { get; set; }
public DateTime MaxDate { get; set; }
public DateRangeAttribute(string minDate, string maxDate)
: base(DefaultErrorMessage)
{
MinDate = ParseDate(minDate);
MaxDate = ParseDate(maxDate);
}
public override bool IsValid(object value)
{
if (value == null || !(value is DateTime))
{
return true;
}
DateTime dateValue = (DateTime)value;
return MinDate <= dateValue && dateValue <= MaxDate;
}
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture,
ErrorMessageString,
name, MinDate, MaxDate);
}
private static DateTime ParseDate(string dateValue)
{
return DateTime.ParseExact(dateValue, DateFormat,
CultureInfo.InvariantCulture);
}
}