2

I have a View that displays the Customer Details.

I would like to display age by the Date of Birth available.

I tried some thing like this.

@Html.DisplayTextFor(model => DateTime.Now.Date.Subtract(model.DOB))

where model.DOB has a value like 7/23/1985 12:00:00 AM

That gives me an error

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

Can you please help me how i can display age with the Date of Birth i have.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
HaBo
  • 13,999
  • 36
  • 114
  • 206

3 Answers3

3

First, figure out how to calculate an age: Calculate age in C#

Then add that code to your view model as a new property (or populate it when you're setting up your view model).

public int Age { get{
    DateTime now = DateTime.Today;
    int age = now.Year - DOB.Year;
    if (DOB > now.AddYears(-age)) age--;
    return age;
}}

Then use that property instead:

@Html.DisplayTextFor(model => model.AGE)
Community
  • 1
  • 1
Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
1

There are two issues here:

  1. This logic should be performed before you get to the view. Your view model should have an Age property on it, since that's what you really want to display. This property should be populated by the controller.
  2. Calculating someone's actual age can be tricky, since you probably don't want to literally show the person's age in terms of the number of 365-day years that they've been alive.

So you should start by modifying your model:

public class MyViewModel
{
    ...
    public TimeSpan Age {get;set;}
}

Then use the correct method to calculate someone's age when you're creating that model:

var model = new MyViewModel
            {
                ...
                Age = CalculateAge(person.DOB, DateTime.Now)
            };

Then modify your View code:

@Html.DisplayTextFor(model => model.Age)
Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Please use `DateTime.UtcNow` to calculate the person's age. You wouldn't want to be dependent upon the current server time, would you ;)? – Marius Schulz Oct 09 '11 at 04:39
  • @MariusSchulz: Actually, ideally I would use Dependency Injection to give me an `IUserDateTimeProvider` implementation that looks at the user's time zone preferences. It's more unit-testable than `DateTime.*`, and I don't like pretending that the world still revolves around Greenwich (with all due respect to Her Majesty). ;-) – StriplingWarrior Oct 09 '11 at 04:44
  • This is perfectly fine, too — `DateTime.Now`, however, is not (which is the only point I tried to make). – Marius Schulz Oct 09 '11 at 04:46
  • @StriplingWarrior This could be a bit complected for me to implement this way as I retrieve Date from using Linq Statement in to a strongly typed Properties and I am not sure how i can add Age property to the linq result set with the above logic. – HaBo Oct 09 '11 at 05:21
  • 1
    @HaBo - And now you know why you shouldn't be passing linq results directly to your view. You run into these problems. – Erik Funkenbusch Oct 09 '11 at 05:37
  • @HaBo: Mystere Man is right. The correct way to do this is to have a ViewModel class that's specifically tailored to represent what you plan to display, so the View code can concern itself entirely with deciding how to display things. The Controller's role is to take information from your business and data access logic, compose a ViewModel, and invoke the View on that ViewModel. You can use a tool like AutoMapper to avoid repetitive code when your ViewModel and the result of the LINQ query are very similar. – StriplingWarrior Oct 09 '11 at 14:17
  • @StriplingWarrior and MystereMan Thanks for your advice.I actually mapped my linq result to the Model. Unfortunately my Dev DB server had some null values for DOB so that was creating a problem for calculating Age, But now i filled it up with Dates. SO thank you very much for all your support here... – HaBo Oct 09 '11 at 17:48
1

you can do like this

@{
var date=DateTime.Now.Date.Subtract(Model.DOB);

@Html.DisplayTextFor(model =>date.Years);
}
Jayantha Lal Sirisena
  • 21,216
  • 11
  • 71
  • 92
  • Jayanth when i try your way i get this value 9574.00:00:00 for a date of 7/23/1985 12:00:00 AM – HaBo Oct 09 '11 at 05:01
  • it is giving compilation error, But below code worked for me. Thank you for your input. that helped me. @{ var age = DateTime.UtcNow.Year - Model.DOB.Year; Age: @Html.DisplayTextFor(model => age); } – HaBo Oct 09 '11 at 05:26
  • @HaBo: You now have enough reputation that you should be able to up-vote answers like this if they helped you. And remember to mark one of the answers as correct. – StriplingWarrior Oct 09 '11 at 14:19