Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I am working on a MVC project, I currently finished three different excel sheets and depending on the type of insurance the user selects a certain default excel sheet format should be used for that insurance.
Currently I can only select one format in the return statement. What I was trying to do is have an if condition depending the string context to decide what excel format to use.

C#
public ActionResult GenerateTripLog(int driverId, int vehicleId, int typeId, int customerId)
{
            try
            {
                var results = (from s in db.usp_TripLog(driverId, vehicleId, typeId, customerId, sDate, eDate) 
                               select s).ToList();

                var driver = new DriverRepository(GetCurrentUser()).GetDriver(driverId);
                var iType = db.insurancetypes.SingleOrDefault(c => c.id == typeId);
                var tenant = GetTenant(GetCurrentUser());                
                var period = "All";           

                var driverSignature = 
                                (from s in driver.driveravailabilityevents
                                where
                                orderby s.createdon descending
                                select s.signature).FirstOrDefault();

                object[] filterStrs =   
                {
                    tenant == null ? "All" : tenant.tenantname,
                    period,
                    driver == null || driver.person == null ? "All" : driver.person.fullname,
                    iType == null ? "All" : iType.name,
                    driverSignature
                };
          //THIS IS WHAT I WAS TRYING TO DO THE SELECTION
                string insuranceType = Convert.ToString(iType);
                if(insuranceType == "FAMILY CARE")
                {
                    return TripLogSpreadsheet(results, filterStrs);
                }
                else if(insuranceType == "BELLIN")
                {
                    return BELLINLogSpreadsheet(results, filterStrs);
                }

      //return TripLogSpreadsheet(results, filterStrs);
      }
      catch (Exception ex)
      {
            ViewData["errormessage"] = ex.Message;
            return View("Error");
      }
}
Posted
Comments
DotNetSteve 17-Nov-15 17:23pm    
Take a look at this link - Might help.
https://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx

Look at FileResult types (FileResult,FileContentResult,FilePathResult,FileStreamResult)
Sinisa Hajnal 18-Nov-15 5:02am    
You should probably override (inherit from) ActionResult with your specific result. But I admit I never had the pleasure of trying it.
TheBigBearNow 18-Nov-15 10:38am    
My issue is my method that GenerateTripLog() wants to return a single value, when I have my ‘if’ condition it will return a value depending on the condition, While the if is being used because the returns are in the if the method throws an error because it says not all paths return a value for the method.
DotNetSteve 18-Nov-15 12:45pm    
You need to return something in the case it is neither of your values - you could return a view in the Finally block or simply at the end of the function.
TheBigBearNow 18-Nov-15 16:18pm    
I believe this may work for me.

Is there a better suggestion?

string insuranceType = Convert.ToString(iType);
string akeString = "";
string logicstr = "";
string bellinStr = "";
if (insuranceType.Length > 2) akeString = insuranceType.Substring(0, 3);
if (insuranceType.Length > 10) logicstr = insuranceType.Substring(0, 11);
if (insuranceType.Length > 10) bellinStr = insuranceType.Substring(0, 11);
if (logicstr == "Logisticare")
{
return TripLogSpreadsheet(results, filterStrs);//logisticare;//
}
else if (akeString.ToUpper() == "ake")
{
return MTMLogSpreadsheet(results, filterStrs);
}
else if (insuranceType == "Bellin")
{
return BellinLogSpreadsheet(results, filterStrs);
}
else
return TripLogSpreadsheet(results, filterStrs);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900