public ActionResult SelectCustomer() { if (Request.IsAuthenticated) { SearchCustomer searchModel = new SearchCustomer(); ViewBag.CustomerTypes = db.CustomerTypes.ToArray<CustomerType>(); return PartialView("SelectCustomer", searchModel); } else { return RedirectToAction("Index", "Home"); } }
public ActionResult SearchCustomer() { SearchCustomer searchModel = new SearchCustomer(); if (TryUpdateModel(searchModel)) { SearchCustomer[] searchResult = null; if (searchModel.RFONumber != null) { var results = from rfo in db.RFONumbers.Where(r => (r.RFONumber1 == searchModel.RFONumber)) join com in db.Companies on rfo.CompanyId equals com.CompanyId join csm in db.CustomerTypes on rfo.CustomerTypeId equals csm.CustomerTypeId select new SearchCustomer { RFONumber = rfo.RFONumber1, SelectedCustomerType = rfo.CustomerType, Name = com.Name, PostCode = rfo.PostCode, BusinessArea = com.BusinessArea }; if (results != null) searchResult = results.ToArray<SearchCustomer>(); } else { var results = from rfo in db.RFONumbers.Where(r => ((searchModel.RFONumber == null || r.RFONumber1 == searchModel.RFONumber) && r.CustomerTypeId == searchModel.SelectedCustomerType.CustomerTypeId && (string.IsNullOrEmpty(searchModel.PostCode) || r.PostCode.Contains(searchModel.PostCode)))) join com in db.Companies.Where(co => ((string.IsNullOrEmpty(searchModel.Name) || co.Name.Contains(searchModel.Name)) && (string.IsNullOrEmpty(searchModel.BusinessArea) || co.BusinessArea.Contains(searchModel.BusinessArea)))) on rfo.CompanyId equals com.CompanyId join csm in db.CustomerTypes on rfo.CustomerTypeId equals csm.CustomerTypeId select new SearchCustomer { RFONumber = rfo.RFONumber1, SelectedCustomerType = rfo.CustomerType, Name = com.Name, PostCode = rfo.PostCode, BusinessArea = com.BusinessArea }; if (results != null) searchResult = results.ToArray<SearchCustomer>(); } if (searchResult != null) return PartialView("SearchCustomerResults", searchResult); else return PartialView("SelectCustomer", new SearchCustomer[0]); } else return PartialView("SearchCustomerResults", new SearchCustomer[0]); }