Error occurring

-1
<td>
  @Html.ActionLink("Editar","EditarPessoa", new { cod=item.Id }) |
  @Html.ActionLink("Details", "Detalhes", new { cod=item.Id }) |
  @Html.ActionLink("Deletar","DeletarPessoa", new { cod=item.Id })
</td>
public ActionResult EditarPessoa(int id)
{
  var pessoaOBJ = pessoaNegocio.BuscarPessoa(id);
  var pessoaVM = ModelParaModel(pessoaOBJ);
  return View(pessoaVM);
}


[HttpPost]
public ActionResult EditarPessoa(ViewModelPessoa modelPessoa)
{
  pessoaNegocio.Alterar(ViewModelParaModel(modelPessoa));
  return View("ListarPessoa","Pessoa");
}

public ActionResult DeletarPessoa(int id)
{
  var pessoaOBJ = pessoaNegocio.BuscarPessoa(id);
  var pessoaVM = ModelParaModel(pessoaOBJ);
  return View(pessoaVM);
}

[HttpDelete]
public ActionResult DeletarPessoa(ViewModelPessoa modelPessoa)
{
  var pessoa = ViewModelParaModel(modelPessoa);
  pessoaNegocio.Deletar(pessoa.Id);
  return View();
}

When you click the delete and edit button it gives the following error:

  

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditPerson (Int32)' in 'ProjectParameters.Controllers.PessoaController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.   Parameter name: parameters

Application Server Error '/'.

  

The number of primary key values passed must match the number of primary key values defined on the entity.   Parameter Name: keyValues   Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.

Exception Details:

  

System.ArgumentException: The number of primary key values passed must match the number of primary key values defined on the entity.   Parameter Name: keyValues

Source Error:

Linha 34:         public T Buscar(params object[] Key)
Linha 35:         {
Linha 36:             return _db.Set<T>().Find(Key);
Linha 37:         }
Linha 38: 

Source File:

  

C: \ Users \ DELL \ Desktop \ Projects_CSharp \ sample_projects \ ProjectDataPartner \ ProjectDataPartner.DAL \ Repository \ GenericRepository.cs Line: 36

Battery Tracking:

[ArgumentException: The number of primary key values passed must match number of primary key values defined on the entity.
Nome do parâmetro: keyValues]
   System.Data.Entity.Internal.WrappedEntityKey..ctor(EntitySet entitySet, String entitySetName, Object[] keyValues, String keyValuesParamName) +429
   System.Data.Entity.Internal.Linq.InternalSet'1.Find(Object[] keyValues) +121
   System.Data.Entity.DbSet'1.Find(Object[] keyValues) +29
   ProjetoDadosPessoa.DAL.Repositorio.GenericRepositorio'1.Buscar(Object[] Key) in C:\Users\DELL\Desktop\Projetos_CSharp\projetos_estudados\ProjetoDadosPessoa\ProjetoDadosPessoa.DAL\Repositorio\GenericRepositorio.cs:36
   ProjetoDadosPessoa.Negocio.Negocio.PessoaNegocio.BuscarPessoa(Int32 id) in C:\Users\DELL\Desktop\Projetos_CSharp\projetos_estudados\ProjetoDadosPessoa\ProjetoDadosPessoa.Negocio\Negocio\PessoaNegocio.cs:42
   ProjetoDadosPessoa.Controllers.PessoaController.DeletarPessoa(Int32 id) in C:\Users\DELL\Desktop\Projetos_CSharp\projetos_estudados\ProjetoDadosPessoa\ProjetoDadosPessoa\Controllers\PessoaController.cs:47
   lambda_method(Closure , ControllerBase , Object[] ) +96
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary'2 parameters) +157
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary'2 parameters) +27
   System.Web.Mvc.Async.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult'2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__11_0() +50
   System.Web.Mvc.Async.<>c__DisplayClass11_1.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
   System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult'1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__3() +35
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__5(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult'1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid'1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid'1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid'1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase'1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9748493
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159

Informações sobre a Versão: Microsoft .NET Framework Versão:4.0.30319; Versão do ASP.NET:4.7.2558.0


da esse erro aqui


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjetoDadosPessoa.DAL.Repositorio.Interface;
using System.Linq.Expressions;
using ProjetoDadosPessoa.DAL.Contexto;
using System.Data.Entity;

namespace ProjetoDadosPessoa.DAL.Repositorio
{
    public class GenericRepositorio<T> : IGenericRepositorio<T>, IDisposable where T : class
    {
        private readonly ProjetoDadosPessoaContexto _db;

        public GenericRepositorio()
        {
            _db = new ProjetoDadosPessoaContexto();
        }

        public void Adicionar(T entidade)
        {
            _db.Set<T>().Add(entidade);
            Commit();
        }

        public void Atualizar(T entidade)
        {
            _db.Entry(entidade).State = EntityState.Modified;
            Commit();
        }

        public T Buscar(params object[] Key)
        {
            return _db.Set<T>().Find(Key);
        }

        public void Commit()
        {
            _db.SaveChanges();
        }

        public void Deletar(int i)
        {
            var obj = Buscar(i);
            _db.Set<T>().Remove(obj);
            Commit();
        }

        public void Dispose()
        {
            if(_db != null)
            {
                _db.Dispose();
            }

            GC.SuppressFinalize(this);
        }

        public List<T> Obter(Expression<Func<T, bool>> predicate)
        {
            return _db.Set<T>().Where(predicate).ToList();
        }

        public List<T> ObterTodos()
        {
            return _db.Set<T>().ToList();
        }

        public T Primeiro(Expression<Func<T, bool>> predicate)
        {
            return _db.Set<T>().Where(predicate).FirstOrDefault();
        }
    }
}
    
asked by anonymous 23.06.2018 / 21:45

1 answer

1

From what I can see in your code snippet, you get the int id parameter in your method, but in your code you pass cod. This causes the parameter to be unrecognized.

You did not show the Code of Details, but I will do it for you in the same way I did for others.

Try the following:

<td>
    @Html.ActionLink("Editar","EditarPessoa", new { id = item.Id }) |
    @Html.ActionLink("Details", "Detalhes", new { id = item.Id }) |
    @Html.ActionLink("Deletar","DeletarPessoa", new { id = item.Id })
</td>
    
24.06.2018 / 21:21