Search code examples
asp.net-core-mvcrepository-patterniformfile

There is no argument given that corresponds to the required formal parameter 'photo' of 'Interface.Create(Trademark, IFormFile)'?


I am using .Net Core 5 and uploading images for my Trademark. I use Repository for my work and got error CS706: There is no argument given that corresponds to the required formal parameter 'photo' of 'Interface.Create(Trademark, IFormFile)' in Controller

_trademarkRepo.CreateNewTrademark(trademark);

Controller

public IActionResult CreateTrademark(Trademark trademark)
    {
        if(ModelState.IsValid)
        {
            _trademarkRepo.CreateNewTrademark(trademark);
        }
        _logger.LogInformation("...");
        return RedirectToAction("Index");
    }

Repo

public bool CreateNewTrademark(Trademark trademark, IFormFile photo)
    {
        var path = Path.Combine(this._webHostEnvironment.WebRootPath, "trademarks", photo.FileName);
        var stream = new FileStream(path, FileMode.Create);
        photo.CopyToAsync(stream);
        if(CheckExist(trademark.TrademarkName))
        {
            return false;
        }
        var newTrademark = new Trademark
        {
            TrademarkName = trademark.TrademarkName,
            Description = trademark.Description,
            Image = photo.FileName
        };
        _dbContext.Trademarks.Add(newTrademark);
        _dbContext.SaveChanges();
        return true;
    }

Solution

  • From error it is evident that what error is.

    1. Method at repo level required two argument. One is trademark and another is photo.

    2. When you have called that from controller , you have only passed one. (Trademark only and photo is missing). This is the error.

    Basically your controller should look like following.

    public IActionResult CreateTrademark(Trademark trademark,IFromFile photo)
        {
            if(ModelState.IsValid)
            {
                _trademarkRepo.CreateNewTrademark(trademark,photo);
            }
            _logger.LogInformation("...");
            return RedirectToAction("Index");
        }
    

    Note: There are many other dependencies like how you post file from UI etc. That is not scope of this question and so answer. You have to look for those detail.