Help with Action Edit and Delete returning null

Asked

Viewed 269 times

3

I am developing an application but I have a problem with Edit and Delete actions. I have looked at the code several times, but calling the cited actions shows the following error:

The Parameters Dictionary contains a null entry for Parameter 'id' of non-nullable type ːSystem.Int32 for method voltar System.Web.Mvc.Actionresult Edit(Int32)' in cheg appXXX.Presentation.Controllers.Xxxcontroller'. An optional Parameter must be a Reference type, a nullable type, or be declared as an optional Parameter.

Name of the parameter: Parameters

What I’m observing is that, for some reason that I haven’t been able to identify yet, the "id" parameter in the cited actions is returning null. Could someone help me? I will post the code below summarized and adapted:

In the DOMAIN Layer:

public interface IRepositoryBase<TEntity> where TEntity : class
{
    IEnumerable<TEntity> GetAll();        
    TEntity GetById(int id);
    void Update(TEntity entity);
    void Save();
    void Add(TEntity entity);
    void Delete(TEntity entity);
    void Dispose();
}

public interface IServiceBase<TEntity> where TEntity : class
{
    IEnumerable<TEntity> GetAll();
    TEntity GetById(int id);
    void Update(TEntity entity);
    void Save();
    void Add(TEntity entity);
    void Delete(TEntity entity);
    void Dispose();
}

public class ServiceBase<TEntity> : IDisposable, IServiceBase<TEntity> where TEntity : class
{
    private readonly IRepositoryBase<TEntity> _repository;

    public ServiceBase(IRepositoryBase<TEntity> repository)
    {
        _repository = repository;
    }

    public void Delete(TEntity entity)
    {
        _repository.Delete(entity);
    }

    public TEntity GetById(int id)
    {
        return _repository.GetById(id);
    }

    public IEnumerable<TEntity> GetAll()
    {
        return _repository.GetAll();
    }
}

In the Infra Layer:

public class RepositoryBase<TEntity> : IDisposable, IRepositoryBase<TEntity> where TEntity : class
{
    protected AppContext context = new AppContext();

    public void Add(TEntity entity)
    {
        context.Set<TEntity>().Add(entity);
    }

    public void Delete(TEntity entity)
    {
        context.Set<TEntity>().Remove(entity);
    }

    public TEntity GetById(int id)
    {
        return context.Set<TEntity>().Find(id);
    }        

    public IEnumerable<TEntity> GetAll()
    {
        return context.Set<TEntity>().ToList();
    }
}

In the APPLICATION Layer:

public interface IAppServiceBase<TEntity> where TEntity : class
{
    IEnumerable<TEntity> GetAll();
    TEntity GetById(int id);
    void Update(TEntity entity);
    void Save();
    void Add(TEntity entity);
    void Delete(TEntity entity);
    void Dispose();
}

public class AppServiceBase<TEntity> : IDisposable, IAppServiceBase<TEntity> where TEntity : class
{
    private readonly IServiceBase<TEntity> _serviceBase;

    public AppServiceBase(IServiceBase<TEntity> serviceBase)
    {
        _serviceBase = serviceBase;
    }

    public void Add(TEntity entity)
    {
        _serviceBase.Add(entity);
    }

    public void Delete(TEntity entity)
    {
        _serviceBase.Delete(entity);
    }

    public TEntity GetById(int id)
    {
        return _serviceBase.GetById(id);
    }

    public IEnumerable<TEntity> GetAll()
    {
        return _serviceBase.GetAll();
    }
}

In the DISPLAY Layer (CONTROLLER):

public class BankController : Controller
{
    private readonly IBankAppService _bankApp;

    public BankController(IBankAppService bankApp)
    {
        _bankApp = bankApp;
    }

    // GET: Bank
    public ActionResult Index()
    {
        var bankViewModel = Mapper.Map<IEnumerable<Bank>, IEnumerable<BankViewModel>>(_bankApp.GetAll());
        return View(bankViewModel);
    }

    // GET: Bank/Edit/5
    public ActionResult Edit(int id)
    {

        var bankDomain = _bankApp.GetById(id);
        var bankViewModel = Mapper.Map<Bank, BankViewModel>(bankDomain);

        return View(bankViewModel);
    }

    //POST: Bank/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(BankViewModel _bank)
    {
        if (ModelState.IsValid)
        {
            var bankDomain = Mapper.Map<BankViewModel, Bank>(_bank);
            _bankApp.Update(bankDomain);
            _bankApp.Save();

            return RedirectToAction("Index");
        }

        return View(_bank);
    }


    // GET: Bank/Delete/5
    public ActionResult Delete(int id)
    {
        var bankDomain = _bankApp.GetById(id);
        var bankViewModel = Mapper.Map<Bank, BankViewModel>(bankDomain);

        return View(bankViewModel);
    }

    // POST: Bank/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        var bankDomain = _bankApp.GetById(id);
        _bankApp.Delete(bankDomain);
        _bankApp.Save();

        return RedirectToAction("Index");
    }
}

As you can see the code is a generic repository and some methods I didn’t post to decrease the size. I’ve done it in several ways based on examples on the internet and projects I downloaded, but then at execution, when you click on the item (a View of type List), the Action Edit is not called pq the id returns null.

The Create Action works and the Getall() tbm function. The problem is with Edit and consequently with Delete tbm. I can’t figure out what’s wrong.

  • Post your View Edit, or check if it has @Html.HiddenFor(model => model.Id).

  • Thanks for the remark. Really was missing in my View get @Html.Hiddenfor(model => model.Id).

1 answer

1

Take a look at your view, when you click the field button id is going null, probably because there is no input name="id" in his view. Or if you have it, it’s null. You can also change the public ActionResult Edit(int id) for public ActionResult Edit(int? id), which will then accept null values. But as it is for editing, I think you really need to pass the value on id. Anything put its view also.

  • Thanks for the remark. Really was missing in my View get @Html.Hiddenfor(model => model.Id).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.