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
71 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 32

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

13 Experts available now in Live!

Get 1:1 Help Now