In the index view a user can search with any combination of one to all of the following parameters: firstname
, lastname
or ssn
. The found search results are displayed in the search results view. In my HomeOfficeController
I have written the code like this, but most of the code is repeating.
Can someone suggest a neater way of writing this code?
[HttpPost]
public ActionResult Index(HomeOfficeViewModel viewModel)
{
TempData["FirstName"] = viewModel.FirstName;
TempData["LastName"] = viewModel.LastName;
TempData["SSN"] = viewModel.FullSsn;
return RedirectToAction("SearchResults", "HomeOffice");
}
public ActionResult SearchResults(HomeOfficeViewModel viewModel, UserSessionContext sessionContext)
{
TempData.Keep();
if (TempData["FirstName"] != null)
{ viewModel.FirstName = TempData["FirstName"].ToString(); }
if (TempData["LastName"] != null)
{ viewModel.LastName = TempData["LastName"].ToString(); }
if (TempData["SSN"] != null)
{ viewModel.FullSsn = TempData["SSN"].ToString(); }
if (viewModel.FirstName != null && viewModel.LastName == null && viewModel.FullSsn == null)
{
var ph = _policyHolderRepository.Where(x => x.FirstName == viewModel.FirstName).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["FirstName"] = "<<< First Name >>> is '" + viewModel.FirstName + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "First Name searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName == null && viewModel.LastName != null && viewModel.FullSsn == null)
{
var ph = _policyHolderRepository.Where(x => x.LastName == viewModel.LastName).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["LastName"] = "<<< Last Name >>> is '" + viewModel.LastName + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "Last Name searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName == null && viewModel.LastName == null && viewModel.FullSsn != null)
{
var ph = _policyHolderRepository.Where(x => x.Ssn == viewModel.FullSsn).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["SSN"] = "<<< Social Security No. >>> is '*****" + viewModel.FullSsn.Substring(5) + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "SSN searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName != null && viewModel.LastName != null && viewModel.FullSsn == null)
{
var ph = _policyHolderRepository.Where(x => x.FirstName == viewModel.FirstName && x.LastName == viewModel.LastName).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["FirstName"] = "<<< First Name >>> is '" + viewModel.FirstName + "'";
ViewData["LastName"] = "<<< Last Name >>> is '" + viewModel.LastName + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "Combination of First Name and Last Name searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName != null && viewModel.LastName == null && viewModel.FullSsn != null)
{
var ph = _policyHolderRepository.Where(x => x.FirstName == viewModel.FirstName && x.Ssn == viewModel.FullSsn).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["FirstName"] = "<<< First Name >>> is '" + viewModel.FirstName + "'";
ViewData["SSN"] = "<<< Social Security No. >>> is '*****" + viewModel.FullSsn.Substring(5) + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "Combination of First Name and SSN searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName == null && viewModel.LastName != null && viewModel.FullSsn != null)
{
var ph = _policyHolderRepository.Where(x => x.LastName == viewModel.LastName && x.Ssn == viewModel.FullSsn).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["LastName"] = "<<< Last Name >>> is '" + viewModel.LastName + "'";
ViewData["SSN"] = "<<< Social Security No. >>> is '*****" + viewModel.FullSsn.Substring(5) + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "The Customer Name and SSN were not found. Please revise your search.");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName != null && viewModel.LastName != null && viewModel.FullSsn != null)
{
var ph = _policyHolderRepository.Where(x => x.FirstName == viewModel.FirstName && x.LastName == viewModel.LastName && x.Ssn == viewModel.FullSsn).ToList();
if (ph.Count != 0)
{
var searchresults = from p in ph
select new SearchResultsViewModel
{
FullSsn = p.Ssn,
FullName = p.FirstName + " " + p.LastName,
UserId = p.UserId,
AccountVerified = p.AccountVerified
};
ViewData["FirstName"] = "<<< First Name >>> is '" + viewModel.FirstName + "'";
ViewData["LastName"] = "<<< Last Name >>> is '" + viewModel.LastName + "'";
ViewData["SSN"] = "<<< Social Security No. >>> is '*****" + viewModel.FullSsn.Substring(5) + "'";
return View("SearchResults", new SearchResultsViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate,
SearchResults = searchresults.ToList()
});
}
else
{
ModelState.Clear();
ModelState.AddModelError("Error", "Combination of First Name, Last Name and SSN searched does not exist in our records");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
}
else if (viewModel.FirstName == null && viewModel.LastName == null && viewModel.FullSsn == null)
{
ModelState.AddModelError("User Error", "Atleast enter one of the search criteria");
var homeOfficeViewModel = new HomeOfficeViewModel()
{
PolicyOwnerFirstName = sessionContext.LoggedInUserFullName,
LastLoggedOnDate = sessionContext.LoggedInUserLastLoggedOnDate
};
return View("Index", homeOfficeViewModel);
}
else
{
return View();
}
}