0

I have this Action in the controller MattinaleController:

public ActionResult Modifica(int id)
{
    // manipulate data from repository

    return RedirectToAction("Modifica", "Modifica");
}

and this is my routing:

routes.MapRoute(
    name: "",
    url: "",
    defaults: new { controller = "Mattinale", action = "Index" }
    );

routes.MapRoute(
    name: "",
    url: "ModificaComunicazione/{IDArticolazione}",
    defaults: new { controller = "Mattinale", action = "Modifica" }
    );

routes.MapRoute(
    name: "",
    url: "{anno}/{mese}/{giorno}",
    defaults: new { controller = "Mattinale", action = "Index" }
    );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

When I start debugging and try to call the route myHost/ModificaComunicazione/5 (random number here), I get this error (translated):

Parameters dictionary contains Null value for parameter 'id' of non nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Modifica(Int32)' in 'Mattinale.WebUI.Controllers.MattinaleController'

Looks like it doesn't take the parameter. What am I missing?!

Thanks, Davide.

Davide Vitali
  • 1,017
  • 8
  • 24

2 Answers2

1

just go through this link. Route config doesn't take the parameter. Param name should be same as which define in actions.

Hardik Dhankecha
  • 148
  • 1
  • 11
0

Davide, Yes you get the id param from the url but not send to the redirection. you should send your id parameter to the RedirectToAction methods. Check out: RedirectToAction with parameter

Seyhmus Gokcen
  • 248
  • 4
  • 10
  • No, I don't get the ID param from the url, that's the point. If I modify the parameter to be optional Modifica(int id = 0), I don't get any error but the id value will always be 0... it is not passed from url to action, there must be something wrong with the routing configuration. – Davide Vitali Jan 09 '19 at 09:34
  • There is something wrong in your clarification that you call **ModificaComunicazione** action and there is nothing about it in your question. So I guess you call it first and in the action you redirect to the second action called **Modifica** But you did not passed int value from the first action to the second one. So you get the error I think. When you set the param as id = 0 you get the default vaue and it is work properly – Seyhmus Gokcen Jan 09 '19 at 09:53
  • I don't call any **ModificaComunicazione** action, that's just an alias for calling the Modifica action in the Mattinale controller directly from url. BTW the question was answered. – Davide Vitali Jan 09 '19 at 10:01