I am doing a CRUD in ASP.NET MVC but my editing method does not work, I am already trying several ways to do it and in my last attempt I get the following error when I click on the edit button.
The parameter dictionary contains a NULL entry for parameter 'idProfile' of non-nullable type 'System.Int32' of method 'System.Web.Mvc.ActionResult Update (Int32)' on 'AdministraZion.Controllers.ProfilesController'. An optional parameter must be a reference type, a nullable type, or declared as an optional parameter. Parameter name: parameters
Here I leave the complete code, I hope you can help me with my problem.
This is my ViewModel (ProfileViewModel)
public class PerfilViewModel
{
public int idPerfil { get; set; }
public string descripcion { get; set; }
public int idEstatus { get; set; }
public IEnumerable<SelectListItem> Estatus { get; set; }
}
This is my Controller (ProfilesController)
public ActionResult Update(int idPerfil)
{
PerfilViewModel vm = new PerfilViewModel();
using (OrganizacionEntities2 db = new OrganizacionEntities2())
{
var t = db.catPerfiles.Find(idPerfil);
vm.descripcion = t.descripcion;
vm.idEstatus = (int)t.idEstatus;
vm.idPerfil = t.idPerfil;
}
ViewBag.Estatus = db.catEstatus.Select(x => new SelectListItem
{
Text = x.descripcion,
Value = x.idEstatus.ToString()
}).ToList();
return View(vm);
}
[HttpPost]
public ActionResult Update(PerfilViewModel vm)
{
try
{
if (ModelState.IsValid)
{
using (OrganizacionEntities2 db = new OrganizacionEntities2())
{
var t = db.catPerfiles.Find(vm.idPerfil);
t.descripcion = vm.descripcion;
t.idEstatus = vm.idEstatus;
db.Entry(t).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
ViewBag.successMessage = "Estatus agregado con exito";
}
return Redirect("/Perfiles/");
}
ViewBag.Estatus = db.catEstatus.Select(x => new SelectListItem
{
Text = x.descripcion,
Value = x.idEstatus.ToString()
}).ToList();
return View(vm);
}
catch (Exception ex)
{
ViewBag.successMessage = "Ocurrio un error, reintente por favor";
throw new Exception(ex.Message);
}
}
This is my View (Update)
@model AdministraZion.ViewModels.PerfilViewModel
@using (Html.BeginForm("Update", "Perfiles", FormMethod.Post))
{
@Html.HiddenFor(d => d.idPerfil)
<div class="box-body">
<div class="form-group">
<label for="inputDescripcion" class="form-control-label mt-3 ml-5">Descripción:</label>
@Html.TextBoxFor(m => m.descripcion, "", new { @class = "form-control ml-5", style = "width:92%" })
</div>
<div class="form-group">
<label for="inputEstatus" class="form-control-label ml-5">Estatus:</label>
@Html.DropDownListFor(m => m.idEstatus, new SelectList(ViewBag.Estatus, "Value", "Text"), "--- Selecciona ---", new { @class = "form-control ml-5", style = "width:92%" })
</div>
</div>
<div class="box-footer" style="text-align:center">
<button type="submit" class="btn block btn-success mb-3" style="width: 30%" name="crear">Crear <i class="fa fa-plus-circle"></i></button>
</div>
}
MyRoute.Config _
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
}
}
Source code error:
An unhandled exception was thrown during the execution of the current web request. Information about the origin and location of the exception can be identified using the following exception stack trace.
This is the stack trace:
[ArgumentException: The parameter dictionary contains a NULL entry for parameter 'profileid' of non-nullable type 'System.Int32' of method 'System.Web.Mvc.ActionResult Update(Int32)' on 'AdministraZion.Controllers. ControllerProfiles'. An optional parameter must be a reference type, a nullable type, or it must be declared as an optional parameter. Parameter name: parameters] System.Web.Mvc.ActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary2 parameters, MethodInfo methodInfo) +527 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +91 System.Web.Mvc .ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,
In your controller, replace:
By:
The reason is in route.config:
If you want another alternative, you can use the [Route] attribute on the controller action, like this:
Recommended reading: https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/