Essentially I'm taking a number of checkboxes off of my view, adding them to a Dictionary<string, bool>
, and writing out the Keys
of any checkbox that is checked.
I'm guessing there must be a better way of doing this.
// We need to build a list of what the user would like more information on.
// First step is to add all of the Checkboxes to a `Dictionary<string, bool>`
Dictionary<string,bool> moreInfo = new Dictionary<string,bool>();
moreInfo.Add("Children's Programs", ccm.moreInfoChildren);
moreInfo.Add("Youth Programs",ccm.moreInfoYouth);
moreInfo.Add("Small Groups", ccm.moreInfoSmallGroups);
moreInfo.Add("Getting involved in Ministry",ccm.moreInfoMinistry);
moreInfo.Add("Receiving offering envelopes",ccm.moreInfoOfferingEnvelopes);
moreInfo.Add("Church Membership",ccm.moreInfoMembership);
moreInfo.Add("Baptism",ccm.moreInfoBaptism);
moreInfo.Add("Other (see notes)", ccm.moreInfoOther);
// Second step is to loop through the moreInfo `Dictionary<string, bool>` and build a string of results.
StringBuilder _moreInfo = new StringBuilder();
foreach (var info in moreInfo) {
_moreInfo.Append(info.Value ? info.Key + ", " : "");
}
// Third step is to determin if the user wants any information. If they do, we add that information to the email.
if(!string.IsNullOrEmpty(_moreInfo.ToString()))
{
builder.Append("I would like more information about ");
builder.Append(_moreInfo.ToString());
}
Here is the complete Action
:
[HttpGet]
[AllowCrossSiteJsonAttribute]
public JsonpResult CommunicateCard(CommunicateCardModel ccm)
{
if (ModelState.IsValid)
{
StringBuilder builder = new StringBuilder();
// ccm.name is a required field, so we don't have to check if it's empty.
builder.Append("Name: " + ccm.name + "\n");
// If these properties are empty, we don't want to include them in the built string.
builder.Append((!string.IsNullOrEmpty(ccm.address)) ? "Address: " + ccm.address + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.city)) ? "City: " + ccm.city + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.postalCode)) ? "Postal Code: " + ccm.postalCode + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.phone)) ? "Phone Number: " + ccm.phone + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.emailFrom)) ? "Email Address: " + ccm.emailFrom + "\n" : null);
builder.Append("\n");
builder.Append((!string.IsNullOrEmpty(ccm.information)) ? "Please " + ccm.information + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.christianity)) ? "I " + ccm.christianity + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.maritalStatus)) ? "I am " + ccm.maritalStatus + "\n" : null);
builder.Append("\n");
// pull together all of the checkboxes within the "Ministries" fieldset and generate a single string.
var pairs = new[]
{
// Tuple.Create(string, bool)
Tuple.Create("Children's Programs", ccm.moreInfoChildren),
Tuple.Create("Youth Programs", ccm.moreInfoYouth),
Tuple.Create("Small Groups", ccm.moreInfoSmallGroups),
Tuple.Create("Getting involved in Ministry", ccm.moreInfoMinistry),
Tuple.Create("Receiving offering envelopes", ccm.moreInfoOfferingEnvelopes),
Tuple.Create("Church Membership", ccm.moreInfoMembership),
Tuple.Create("Baptism", ccm.moreInfoBaptism),
Tuple.Create("Other (see notes)", ccm.moreInfoOther),
};
var moreInfo = String.Join(", ", pairs.Where(pair => pair.Item2).Select(pair => pair.Item1));
if (!String.IsNullOrEmpty(moreInfo))
{
builder.Append("I would like more information about ")
.Append(moreInfo);
}
builder.Append("\n");
builder.Append((!string.IsNullOrEmpty(ccm.guestReason)) ? "I am visiting today because " + ccm.guestReason + "\n" : null);
builder.Append((!string.IsNullOrEmpty(ccm.attendance)) ? "I am attending for my " + ccm.attendance + "\n" : null);
builder.Append("\n");
builder.Append((!string.IsNullOrEmpty(ccm.comments)) ? ccm.comments : null);
// initiate the email service and fire off an email
EmailService email = new EmailService();
email.to = ccm.emailTo;
email.from = ccm.emailTo;
email.replyTo = ccm.emailFrom;
email.subject = ccm.messageSubject;
email.messageBody = builder.ToString();
bool sent = email.send();
object result = null;
// Send a system error message via JSON back to the app if the email fails.
if (!sent)
{
result = new ErrorsModel { ErrorMessage = Server.GetLastError().Message };
}
// whether or not the email sent, we need to tell the app the result.
return this.Jsonp(result, sent);
}
// The ModelState is invalid, Loop through the ModelState and send a JSON string listing all the errors.
else
{
List<ErrorsModel> Errors = new List<ErrorsModel>();
foreach(ModelState modelState in ModelState.Values)
{
foreach (ModelError error in modelState.Errors)
{
Errors.Add(new ErrorsModel { ErrorMessage = error.ErrorMessage });
};
};
return this.Jsonp(Errors, false);
}
}