Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables with similar data for body insurance and third party car insurance ... I have used enum in the model to separate the insurances and I want to do the creation operation for it .... There are two modes for each insurance. One case when that car does not have insurance yet and the second case when we want to extend it. I wrote this code to create the form, but it encounters the following error I also get an error on the name of the Create function.error = not all code paths return a value. Please advise


What I have tried:

public async Task<IActionResult> Create(int id, int type)
    {
        InsuranceViewModel model;
        ViewBag.Type = type;

        var companies = await _context.InsuranceCompany
            .Where(e => e.IsActice)
            .ToListAsync();
        ViewData["CompanyList"] = new SelectList(companies, "Id", "CompanyName");

        if ((InsuranceType)type == InsuranceType.Body)
        {
            var bodyInsurance = await _context.BodyInsurance
           .Include(e => e.InsuranceCompany)
           .FirstOrDefaultAsync(e => e.Id == id);

            if (bodyInsurance == null)
            {
                model = new InsuranceViewModel
                {
                    CompanyId = bodyInsurance.InsuranceCompanyId,
                    CompanyName = bodyInsurance.InsuranceCompany.CompanyName,
                    InsuranceType = InsuranceType.Body,
                    IssueDate = new DateTime(bodyInsurance.IssueDate).Ticks,
                    ExpireDate = new DateTime(bodyInsurance.ExpireDate).Ticks,

                    VehicleInformationId = id
                };
            }
            else
            {
                var lastBody = await _context.BodyInsurance.Include(e => e.InsuranceCompany)
                        .Where(e => e.VehicleInformationId == id)
                        .OrderBy(e => e.ExpireDate)
                        .LastAsync();
                model = new InsuranceViewModel
                {
                    ExpireDate = new DateTime(lastBody.ExpireDate).AddYears(1).AddDays(1).Ticks,
                    CompanyId = lastBody.InsuranceCompanyId,
                    CompanyName = lastBody.InsuranceCompany.CompanyName,
                    InsuranceType = InsuranceType.Body,
                    IssueDate = new DateTime(lastBody.ExpireDate).AddDays(1).Ticks,
                    VehicleInformationId = id
                };

            }
        }
        else
        {
            if ((InsuranceType)type == InsuranceType.Thirdpart)
            {
                var thirdParty = await _context.ThirdPartyInsurance
                   .Include(e => e.InsuranceCompany)
                   .FirstOrDefaultAsync(e => e.Id == id);

                if (thirdParty == null)
                {
                    model = new InsuranceViewModel
                    {
                        CompanyId = thirdParty.InsuranceCompanyId,
                        CompanyName = thirdParty.InsuranceCompany.CompanyName,
                        InsuranceType = InsuranceType.Body,
                        IssueDate = new DateTime(thirdParty.IssueDate).Ticks,
                        ExpireDate = new DateTime(thirdParty.ExpireDate).Ticks,
                        VehicleInformationId = id
                    };
                }

                else
                {
                    var lastThirdParty = await _context.ThirdPartyInsurance.Include(e => e.InsuranceCompany)
                         .Where(e => e.VehicleInformationId == id)
                         .OrderBy(e => e.ExpireDate)
                         .LastAsync();
                    model = new InsuranceViewModel
                    {
                        ExpireDate = new DateTime(lastThirdParty.ExpireDate).AddYears(1).AddDays(1).Ticks,
                        CompanyId = lastThirdParty.InsuranceCompanyId,
                        CompanyName = lastThirdParty.InsuranceCompany.CompanyName,
                        InsuranceType = InsuranceType.Body,
                        IssueDate = new DateTime(lastThirdParty.ExpireDate).AddDays(1).Ticks,
                        VehicleInformationId = id
                    };

                }
            }

            return View(model);
        }
Posted
Updated 15-Jun-20 0:08am

Well, its horribly expressed logic - I'd split out your if-elses into the ?4 cases, at worst using a switch, at best using seperate procedures

I suspect the issue is, your two 'major' if-else conditions being

if ((InsuranceType)type == InsuranceType.Body)
and it's lower 'else' block, in the lower else block you
return View(model);
but you don't for the top
if ((InsuranceType)type == InsuranceType.Body)
block

If the code were arranged more nicely, you'd see that, but as it is, it's hard to spot
 
Share this answer
 
Comments
Member 14615938 26-May-20 4:28am    
I have to consider 4 modes for body insurance twice and for third party insurance twice
Garth J Lancaster 26-May-20 6:28am    
sure, but your code style is horrible - if I were reviewing this it would fail
Member 14615938 26-May-20 23:57pm    
Do you have a better solution?
Garth J Lancaster 27-May-20 0:16am    
Well, first thing is always to make it work .. add the missing
return View(model); 
so the top block has it, ie
        if ((InsuranceType)type == InsuranceType.Body)
        {
            var bodyInsurance = await _context.BodyInsurance
           .Include(e => e.InsuranceCompany)
           .FirstOrDefaultAsync(e => e.Id == id);

            if (bodyInsurance == null)
            {
                 …
            }
            else
            {
                 …
            }
            return View(model);    // Missing return !!!!
        }
        else
and leave the rest of the code for now. I'll post back later with a cleaner option
Member 14615938 27-May-20 0:39am    
I encountered this error when I added the code
error: ambigupus match exception : the request matched multiple endpoint
Quote:
not all code paths return a value.

This mean that some part of code have a return, and other parts don't.
In your case, a } is missing before the return, and it makes it part of the else.
C#
    }

    return View(model);
} // this } is the end of the else
 
Share this answer
 
// GET: VehicleInformations/Create
public IActionResult Create()
{

    var typelistdocumentstatus = _context.DocumentStatus.ToList();
    typelistdocumentstatus.Insert(0, new DocumentStatus { Id = -1, Status = "انتخاب کنید" });
    ViewData["DocumentStatusId"] = new SelectList(typelistdocumentstatus, "Id", "Status");

    var typelistoffice = _context.Office.ToList();
    typelistoffice.Insert(0, new Office { Id = -1, Name = "انتخاب کنید" });
    ViewData["OfficeId"] = new SelectList(typelistoffice, "Id", "Name");

    var typelistunit = _context.Unit.ToList();
    typelistunit.Insert(0, new Unit { Id = -1,UnitName = "انتخاب کنید" });
    ViewData["UnitId"] = new SelectList(typelistunit, "Id", "UnitName");

    var typelistsvehicletype = _context.VehicleType.ToList();
    typelistsvehicletype.Insert(0, new VehicleType { Id = -1, TypeName = "انتخاب کنید" });
    ViewData["VehicleTypeId"] = new SelectList(typelistsvehicletype, "Id", "TypeName");

    return View();
}

// POST: VehicleInformations/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,VehicleTypeId,UnitId,VehicleColor,Vin,EngineNumber,NumberPlate,DocumentStatusId,OfficeId,OwnerStatus,OwnerName,Description")] VehicleInformation vehicleInformation)
{
    if (ModelState.IsValid)
    {
        _context.Add(vehicleInformation);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    ViewData["DocumentStatusId"] = new SelectList(_context.DocumentStatus, "Id", "Status", vehicleInformation.DocumentStatusId);
    ViewData["OfficeId"] = new SelectList(_context.Office, "Id", "Name", vehicleInformation.OfficeId);
    ViewData["UnitId"] = new SelectList(_context.Unit, "Id", "UnitName", vehicleInformation.UnitId);
    ViewData["VehicleTypeId"] = new SelectList(_context.VehicleType, "Id", "TypeName", vehicleInformation.VehicleTypeId);
    return View(vehicleInformation);
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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