Estou desenvolvendo uma aplicação mas estou com um problema nas Actions Edit e Delete. Já olhei o código diversas vezes, mas ao chamar as actions citadas apresenta o seguinte erro:
The parameters dictionary contains a null entry for parameter ‘id’ of non-nullable type ‘System.Int32′ for method ‘System.Web.Mvc.ActionResult Edit(Int32)’ in ‘appXXX.Presentation.Controllers.XXXController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Nome do parâmetro: parameters
O que estou observando é que, por algum motivo que ainda não consegui identificar, o parametro “id” nas actions citadas está retornando nulo. Alguém poderia me ajudar? Vou postar o código abaixo resumido e adaptado:
Na Camada de DOMAIN:
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();
}
}
Na Camada de INFRA:
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();
}
}
Na Camada de APPLICATION:
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();
}
}
Na Camada de APRESENTAÇÃO (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");
}
}
Como podem ver o código é um repositório genérico e alguns métodos não postei para diminuir o tamanho. Já fiz de várias formas baseados em exemplos na internet e projetos que baixei, mas aí na execução, quando clica no no item (uma View do tipo List), a Action Edit não é chamada pq o id retorna nulo.
A Action de Create funciona e a função GetAll() tbm. O problema mesmo é com o Edit e consequentemente com o Delete tbm. Não consigo perceber o que tem de errado.
View
Edit, ou verifique se ela possui@Html.HiddenFor(model => model.Id)
.