I have defined the following Calculated property inside my Model class and i define it to have only get (without set):-
public partial class Visit : IValidatableObject
{
public int AgeInYears
{
get
{ int years = Date.Year - Patient.DateOfBirth.Year;
if (Date.Month < Patient.DateOfBirth.Month ||
(Date.Month == Patient.DateOfBirth.Month &&
Date.Day < Patient.DateOfBirth.Day))
{ years--; }
return years; }
But if i try to add a new visit then the following error (System.NullReferenceException was unhandled by user code) will be raised on the Patient inside the following line of code
int years =Date.Year-Patient.DateOfBirth.Year;
But my question is why the AgeInYears property is being executed when i created a new visit object, (although i define it to have a
get;
without
set;
), so i think it should only be executed when i explicitly call it??? BR
There is an old discussion in forum on this issue that default model binder always calls the properties for validation and that could be the reason it is throwing error.
We investigated this and have concluded that the validation system is behaving as expected. Since model validation involves attempting to run validation over
all properties, and since non-nullable value type properties have an implicit [Required] attribute, we're validating this property and calling its getter in the process. We understand that this is a breaking change from V1 of the product, but it's necessary
to make the new model validation system operate correctly.
You have a few options to work around this. Any one of these should work:
Change the Date property to a method instead of a property; this way it will be ignored by the MVC framework.
Change the property type to DateTime? instead of DateTime. This removes the implicit [Required] from this property.
Clear the static DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes flag. This removes the implicit [Required] from all non-nullable value type properties application-wide.
We're considering adding in V3 of the product an attribute which will signal to us "don't bind it, don't validate it, just pretend that this property doesn't exist."
johnjohn1231...
Participant
922 Points
871 Posts
Why a calculated property is being executed when a model is created
May 25, 2012 10:50 PM|LINK
I have defined the following Calculated property inside my Model class and i define it to have only get (without set):-
public partial class Visit : IValidatableObject { public int AgeInYears { get { int years = Date.Year - Patient.DateOfBirth.Year; if (Date.Month < Patient.DateOfBirth.Month || (Date.Month == Patient.DateOfBirth.Month && Date.Day < Patient.DateOfBirth.Day)) { years--; } return years; }But if i try to add a new visit then the following error (System.NullReferenceException was unhandled by user code) will be raised on the Patient inside the following line of code
But my question is why the AgeInYears property is being executed when i created a new visit object, (although i define it to have a
without ), so i think it should only be executed when i explicitly call it??? BRkshyju
Member
134 Points
39 Posts
Re: Why a calculated property is being executed when a model is created
May 25, 2012 11:15 PM|LINK
Are you accessing this property in the constructor of Visit class ?
My flarack profile
johnjohn1231...
Participant
922 Points
871 Posts
Re: Why a calculated property is being executed when a model is created
May 26, 2012 12:16 AM|LINK
I do not think so; as my Get & Post action methods look as follow:-
public ActionResult Create(int patientid) { Visit visit = new Visit(); visit.PatientID = patientid; visit.CreatedBy = User.Identity.Name; visit.Date = DateTime.Now; return View(visit); } // // POST: /Visit/Create [HttpPost] public ActionResult Create(Visit visit) { if (ModelState.IsValid) { if (Roles.IsUserInRole(visit.DoctorID, "Doctor")) { visit.StatusID = repository.GetVisitStatusByDescription("Assinged"); visit.CreatedBy = User.Identity.Name; visit.Date = DateTime.Now; repository.AddVisit(visit); repository.Save(); return RedirectToAction("Index"); } else{return View("NotFound");} } visit.CreatedBy = User.Identity.Name; visit.Date = DateTime.Now; return View(visit); }so i am not referencing the property any where ...
BR
CPrakash82
All-Star
18270 Points
2839 Posts
Re: Why a calculated property is being executed when a model is created
May 26, 2012 12:17 AM|LINK
There is an old discussion in forum on this issue that default model binder always calls the properties for validation and that could be the reason it is throwing error.
http://forums.asp.net/t/1523362.aspx
Response from MVC Team.
We investigated this and have concluded that the validation system is behaving as expected. Since model validation involves attempting to run validation over all properties, and since non-nullable value type properties have an implicit [Required] attribute, we're validating this property and calling its getter in the process. We understand that this is a breaking change from V1 of the product, but it's necessary to make the new model validation system operate correctly.
You have a few options to work around this. Any one of these should work:
Thanks