Error that is occurring

Asked

Viewed 58 times

-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 click delete button and edit from 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 Editarpessoa(Int32)' in 'Projectpessoa.Controllers.Pessoacontroller'. An optional Parameter must be a Reference type, a nullable type, or be declared as an optional Parameter. Name of the parameter: Parameters


Server Error in Application '/'.

The number of Primary key values passed must match number of Primary key values defined on the Entity. Parameter name: keyValues Description: An untreated exception occurred during the execution of the current web request. Examine stack tracking for more information about the error and where it originated in the code.

Details of the Exception:

System.Argumentexception: The number of Primary key values passed must match number of Primary key values defined on the Entity. Parameter name: keyValues

Error of Origin:

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

Source Archive:

C: Users DELL Desktop Projects_csharp projects_studied projects_person Projectodaperson.DAL Repositorio Genericrepositorio.Cs Line: 36

Cell Trace:

[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();
        }
    }
}

1 answer

1

From what I can see in your code snippet, you get in your method the parameter int id, but in your code you pass Cod. This makes the parameter not recognized.

You didn’t show the Details code, but I’ll do it for him the same way I did for the others.

Try to do 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>

Browser other questions tagged

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