Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Deleting operations in the section is difficult

The third party insurance part is removed, but the body insurance part is not removed

And that I want to be returned to the screen Index with the InsuranceController after the removal operation, which doesn't happen


What I have tried:

// GET:Insurances/Delete/5
public async Task<IActionResult> Delete(int id, int type)
{
    InsuranceViewModel model;

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

        model = new InsuranceViewModel
        {
            ExpireDate = bodyInsurance.ExpireDate,
            CompanyId = bodyInsurance.InsuranceCompanyId,
            CompanyName = bodyInsurance.InsuranceCompany.CompanyName,
            InsuranceType = InsuranceType.Body,
            IssueDate = bodyInsurance.IssueDate,
            VehicleInformationId = id
        };
    }
    else
    {
        var thirdParty = await _context.ThirdPartyInsurance
            .Include(e => e.InsuranceCompany)
            .FirstOrDefaultAsync(e => e.Id == id);
        if (thirdParty == null)
        {
            return NotFound();
        }

        model = new InsuranceViewModel
        {
            ExpireDate = thirdParty.ExpireDate,
            CompanyId = thirdParty.InsuranceCompanyId,
            CompanyName = thirdParty.InsuranceCompany.CompanyName,
            InsuranceType = InsuranceType.Thirdpart,
            IssueDate = thirdParty.IssueDate,
            VehicleInformationId = id
        };
    }
    return View(model);
}

// POST: Insurances/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id, int type)
{

    if ((InsuranceType)type == InsuranceType.Body)
    {

        var bodyInsurance = await _context.BodyInsurance.FindAsync(id);
        if (bodyInsurance == null)
        {
            return NotFound();
        }
        _context.BodyInsurance.Remove(bodyInsurance);
        await _context.SaveChangesAsync();



    }
    else
    {
        var thirdParty = await _context.ThirdPartyInsurance.FindAsync(id);
        if (thirdParty == null)
        {
            return NotFound();
        }
        _context.ThirdPartyInsurance.Remove(thirdParty);
        await _context.SaveChangesAsync();
    }

    return RedirectToAction("VehicleInformations", "Index");
}
Posted
Updated 15-Jun-20 0:06am

Quote:
I want to be returned to the screen Index with the InsuranceController after the removal operation
...
C#
return RedirectToAction("VehicleInformations", "Index");
The RedirectToAction method[^] expects the first parameter to be the action, and the second parameter to be the controller.

You have told your code to redirect to the VehicleInformations action on the Index controller.

If you want to redirect to the Index action in the Insurance controller instead, change your code to:
C#
return RedirectToAction("Index", "Insurance");
If the action you're redirecting to is in the same controller as the current action, you can simplify that to:
C#
return RedirectToAction(nameof(Index));


Quote:
The third party insurance part is removed, but the body insurance part is not removed
That's going to depend on your data, and on the values you're posting back to the action. We don't have access to your database, so we can't debug the problem for you.

Set a breakpoint on the first line of the DeleteConfirmed method, and step through it to make sure the parameters are being populated correctly, and that the ID refers to a record which exists in your database.
 
Share this answer
 
v2
Comments
Maciej Los 27-May-20 16:16pm    
Well explained!
public async Task<IActionResult> Delete(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var vehicleInformation = await _context.VehicleInformation
        .Include(v => v.DocumentStatus)
        .Include(v => v.Office)
        .Include(v => v.Unit)
        .Include(v => v.VehicleType)
        .FirstOrDefaultAsync(m => m.Id == id);
    if (vehicleInformation == null)
    {
        return NotFound();
    }

    var viewmodel = VehicleInformationViewModel.FromInformationVehicle(vehicleInformation, _context);
    return View(viewmodel);
}

// POST: VehicleInformations/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
    var vehicleInformation = await _context.VehicleInformation.FindAsync(id);
    _context.VehicleInformation.Remove(vehicleInformation);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}
 
Share this answer
 
Comments
CHill60 15-Jun-20 8:41am    
I think you meant to post this at Asp.net core3 error: not all code paths return a value[^] and vice versa

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