Solved

How do I create properties in a model class that are derived from other values in that class?

Posted on 2015-02-20
2
74 Views
Last Modified: 2015-02-20
I have a ViewModel that contains two entities as shown below:

public class ClientMaxActivityViewModel
    {
        public Client clientData { get; set; }
        public Activity activityData { get; set; }

    }

I want to create a property in this model that is dependent on the values of properties in both the Client and Activity entities.   It would be a string with a value of either 'Compliant' or 'Non-compliant'. Here is some pseudo-code for the logic (I  know the syntax is all wrong, but hopefully the principal is evident):

public string Compliant
{
      if(Client.VisitFrequency == "Monthly" && DateDiff(Activity.ActivityDate - Date(Now)) > 31)
           { return "Non-compliant"
             else
          return "Compliant"}
}

Thanks.
0
Comment
Question by:MinirvaTwo
2 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40621791
If you will be using this property in any decision logic, may I recommend using a boolean property instead:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28621250
{
	class Program
	{
		static void Main(string[] args)
		{
			ClientMaxActivityViewModel model = new ClientMaxActivityViewModel() { clientData = new Client() { VisitFrequency = VisitFrequency.Monthly }, activityData = new Activity() { ActivityDate = DateTime.Now.AddMonths(1) } };
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			model.activityData.ActivityDate = DateTime.Now.AddMonths(-1);
			Console.WriteLine("Is model compliant for Activity Date of {0} - {1}", model.activityData.ActivityDate.ToString("MM/dd/yyyy"), model.IsCompliant);
			Console.ReadLine();
		}
	}

	class ClientMaxActivityViewModel
	{
		public Client clientData { get; set; }
		public Activity activityData { get; set; }
		public bool IsCompliant
		{
			get 
			{
				if (clientData != null && activityData != null)
				{
					switch (clientData.VisitFrequency)
					{
						case VisitFrequency.Daily:
							return activityData.ActivityDate.AddDays(1) > DateTime.Now;
						case VisitFrequency.Weekly:
							return activityData.ActivityDate.AddDays(7) > DateTime.Now;
						case VisitFrequency.Monthly:
							return activityData.ActivityDate.AddMonths(1) > DateTime.Now;
						case VisitFrequency.Annually:
							return activityData.ActivityDate.AddYears(1) > DateTime.Now;
						case VisitFrequency.None:
						default:
							return false;
					}
				}
				return false;
			}
		}
	}

	class Client
	{
		public VisitFrequency VisitFrequency { get; set; }
	}

	class Activity
	{
		public DateTime ActivityDate { get; set; }
	}

	enum VisitFrequency
	{
		None = 0,
		Daily = 1,
		Weekly = 2,
		Monthly = 3,
		Annually = 4
	}
}

Open in new window

-saige-
0
 

Author Closing Comment

by:MinirvaTwo
ID: 40622001
Thanks it_saige!  You rock!  Worked perfectly.  I used text values in my switch/case statements instead of the enums, but it all worked great.  I could reference all of the properties of the ViewModel in the View, as shown below:

<td>
            @Html.DisplayFor(modelItem => item.clientData.Status)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.activityData.ActivityDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsCompliant)
        </td>

Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now