Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I get this error when i added this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using LeaveManagement.Data;
using LeaveManagement.Models;
using AutoMapper;
using LeaveManagement.Contracts;
using LeaveManagement.Repositories;
using Microsoft.AspNetCore.Authorization;
using LeaveManagement.Constants;
using Microsoft.AspNetCore.Identity;

namespace LeaveManagement.Controllers
{
    [Authorize]
    public class LeaveRequestsController : Controller
    {
        private readonly ILeaveRequestRepository leaveRequestRepository;
        private readonly ILeaveTypeRepository leaveTypeRepository;
        private readonly ILogger<LeaveRequestsController> logger;
        private readonly IWebHostEnvironment webHost;

        public LeaveRequestsController(ILeaveRequestRepository leaveRequestRepository, ILeaveTypeRepository leaveTypeRepository,
            ILogger<LeaveRequestsController> logger,
             IWebHostEnvironment webHost)
        {
            this.leaveRequestRepository = leaveRequestRepository;
            this.leaveTypeRepository = leaveTypeRepository;
            this.logger = logger;
            this.webHost = webHost;
        }

        [Authorize(Roles = Roles.Administrator + "," + Roles.Supervisor)]
        // GET: LeaveRequests
        public async Task<IActionResult> Index()
        {
            var model = await leaveRequestRepository.GetAdminLeaveRequestList();
            return View(model);
        }

        public async Task<ActionResult> MyLeave()
        {
            var model = await leaveRequestRepository.GetMyLeaveDetails();
            return View(model);
        }

        // GET: LeaveRequests/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            var model = await leaveRequestRepository.GetLeaveRequestAsync(id);
            if (model == null)
            {
                return NotFound();
            }
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> ApproveRequest(int id, bool approved)
        {
            try
            {
                await leaveRequestRepository.ChangeApprovalStatus(id, approved);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error Approving Request, Contact mpa for support");
                throw;
            }
            return RedirectToAction(nameof(Index));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Cancel(int id)
        {
            try
            {
                await leaveRequestRepository.CancelLeaveRequest(id);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error Cancelling Request, Contact a for support");

                throw;
            }
            return RedirectToAction(nameof(MyLeave));
        }
        [HttpGet]
        // GET: LeaveRequests/Create
        public async Task<IActionResult> Create()
        {
            var model = new LeaveRequestCreateVM
            {
                LeaveTypes = new SelectList(await leaveTypeRepository.GetAllAsync(), "Id", "Name"),
            };
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> Create(IFormFile file)
        {
            string uploadFolder = Path.Combine(webHost.WebRootPath, "uploads");

            if (!Directory.Exists(uploadFolder))
            {
                Directory.CreateDirectory(uploadFolder);
            }
            string fileName = Path.GetFileName(file.FileName);
            string fileSavePath = Path.Combine(uploadFolder, fileName);
            using (FileStream stream = new FileStream(fileSavePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
            return View();
        }

        // POST: LeaveRequests/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(LeaveRequestCreateVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var isValidRequest = await leaveRequestRepository.CreateLeaveRequest(model);
                    if (isValidRequest)
                    {
                        return RedirectToAction(nameof(MyLeave));
                    }
                    ModelState.AddModelError(string.Empty, "You have exceeded your allocation with this request.");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "An Error Has Occurred. Please Try Again Later Or Contact ma for support");
            }

            model.LeaveTypes = new SelectList(await leaveTypeRepository.GetAllAsync(), "Id", "Name", model.LeaveTypeId);
            return View(model);
        }
    }
}


What I have tried:

I tried adding the code in the origin constructor though i am failing, please help
Posted
Updated 2-May-24 22:38pm
v3
Comments
Richard Deeming 3-May-24 4:26am    
The error means you have multiple actions which match the request you're making.

But since you've only shown one action, haven't shown the request, and have truncated the error message, nobody can help you.
Gift Sefike 3-May-24 4:38am    
Sorry, I updated the quetion
Dave Kreskowiak 3-May-24 18:07pm    
For future reference, copy and paste the ENTIRE error message. It would have pointed to the methods the error is talking about.

1 solution

Quote:
C#
[HttpPost]
public async Task<IActionResult> Create(IFormFile file)

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(LeaveRequestCreateVM model)
You have two actions which map to POST: LeaveRequests/Create; the server has no way to determine which action you want to call.

You'll need to rename or remove one of those actions.
 
Share this answer
 

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