Route problem with MVC 5 C#

Asked

Viewed 186 times

0

I’ve already researched it, I’ve spent two hours burning the dog but I couldn’t find a solution. Has anyone ever had that problem?

I have the following link:

http://localhost:49609/Manufacturer/Editarregistro/53/1/4/G/3/13

Controller -> Fabricante
Action -> EditarRegistro
Param01 ->53
Param02 ->1
Param03 ->4
...
Param05 -> 13.

My route is configured as follows:

  ///ROTAS PARA FABRICANTE
            routes.MapRoute(
                    name: "EditarFabricante",
                    url: "{controller}/" +
                         "{action}/" +
                         "{org_tab_in_codigo}/" +
                         "{org_pad_in_Codigo}/" +
                         "{org_in_codigo}/" +
                         "{Org_Tau_In_Codigo}/" +
                         "{vFab_In_Codigo}/" +
                         "{Alf_In_codigo}",
                    defaults: new
                    {
                        controller = "Fabricante",
                        action = "EditarRegistro",
                        org_tab_in_codigo = UrlParameter.Optional,
                        org_pad_in_Codigo = UrlParameter.Optional,
                        org_in_codigo = UrlParameter.Optional,
                        Org_Tau_In_Codigo = UrlParameter.Optional,
                        vFab_In_Codigo = UrlParameter.Optional,
                        Alf_In_codigo = UrlParameter.Optional
                    });

Man controller is configured:

    [Route("EditarFabricante")]
    public ActionResult EditarRegistro(int Org_Tab_In_Codigo,
                                       int Org_Pad_In_Codigo,
                                       int Org_In_Codigo,
                                       string Org_Tau_In_Codigo,
                                       string vFab_In_Codigo,
                                       int Alf_In_Codigo)
    {
        ViewBag.NotView = ViewData.vNotView;
        var appFabricante = new WebEstFabricanteAplicacao();
        var webFabricante = appFabricante.ListarUmRegistrodaOrganizacao(Org_Tab_In_Codigo,
                                                                        Org_Pad_In_Codigo,
                                                                        Org_In_Codigo,
                                                                        Org_Tau_In_Codigo,
                                                                        int.Parse(vFab_In_Codigo.ToString()),
                                                                        Alf_In_Codigo);

        return View(webFabricante);
    }

[EDIT]

Man button to edit the record is as follows:

                                                       <a href="@Url.RouteUrl("EditarFabricante", 
                                    new {action = "EditarRegistro",
                                    org_tab_in_codigo = item.Org_Tab_In_Codigo,
                                    org_pad_in_Codigo = item.Org_Pad_In_Codigo,
                                    org_in_codigo = item.Org_In_Codigo,
                                    Org_Tau_In_Codigo = item.Org_Tau_St_Codigo,
                                    vFab_In_Codigo = item.Fab_In_Codigo,
                                    Alf_In_codigo = item.Alf_In_Codigo})"
                               title="Editar Registro"
                               onclick="Loading();"
                               class="btn btn-primary">
                                <i class="fa fa-edit"></i>
                            </a>

[/EDIT]

By clicking on button it presents the error: Erro

I can’t decipher because damn, my controller is taking the value of vFAB_IN_CODIGO as null, and it is identified in the URL.

inserir a descrição da imagem aqui

[EDITION]

Now I discovered that the problem occurs in the second ROUTE created, when I call the first custom ROUTE the system performs normally, but when executing another ROUTE with the same parameters the system displays the null parameter error.

        ///ROTAS PARA FABRICANTE
        routes.MapRoute(
                name: "EditarFabricante",
                url: "{controller}/" +
                     "{action}/" +
                     "{org_tab_in_codigo}/" +
                     "{org_pad_in_Codigo}/" +
                     "{org_in_codigo}/" +
                     "{Org_Tau_In_Codigo}/" +
                     "{vFab_In_Codigo}/" +
                     "{Alf_In_codigo}",
                defaults: new
                {
                    controller = "Fabricante",
                    action = "EditarRegistro",
                    org_tab_in_codigo = UrlParameter.Optional,
                    org_pad_in_Codigo = UrlParameter.Optional,
                    org_in_codigo = UrlParameter.Optional,
                    Org_Tau_In_Codigo = UrlParameter.Optional,
                    vFab_In_Codigo = UrlParameter.Optional,
                    Alf_In_codigo = UrlParameter.Optional
                });


        routes.MapRoute(
                name: "ExcluirFabricante",
                url: "{controller}/{action}/{org_tab_in_codigo}/{org_pad_in_Codigo}/{org_in_codigo}/{Org_Tau_In_Codigo}/{vFab_In_Codigo}/{Alf_In_codigo}",
                defaults: new
                {
                    controller = "Fabricante",
                    action = "ExcluirRegistro",
                    org_tab_in_codigo = UrlParameter.Optional,
                    org_pad_in_Codigo = UrlParameter.Optional,
                    org_in_codigo = UrlParameter.Optional,
                    Org_Tau_In_Codigo = UrlParameter.Optional,
                    vFab_In_Codigo = UrlParameter.Optional,
                    Alf_In_codigo = UrlParameter.Optional
                });

Running the first route:

inserir a descrição da imagem aqui

When clicking normally calls to the edit ROUTE. inserir a descrição da imagem aqui

Now by clicking delete which calls the second route with the same parameters, but Action different from the error.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

I’m doing it wrong?

[/EDITION]

  • Tried instead of getting string vFab_In_Codigo receive int vFab_In_Codigo. In the error message it says that it is not coming whole, maybe for some reason(supernatural) it is not converting int to string

  • In fact he is int I put it as a string to check the value that was returning, but it always returns as null. [link]https://imgur.com/a/g34oyD6

  • vFab_In_Codigo is int or int? ???

  • "int", as it is a record edit button it has to pass the/ composed keys to find this record in the database.

  • and you noticed that in the view you assign item.Org_Tau_St_Codigo for Org_Tau_In_Codigo, Is this difference in name right? I built your structure here and got no error.

  • Leandro, I verified that the problem occurs in the second route onwards. I edited the post with some new details that I detected.

  • The two routes have the same number of parameters; how do you think MVC will be able to solve which is which?

  • Marcelo thinking about what you said, I reached another conclusion/solution. I will post the {GATO} that was made to work this.

Show 3 more comments

1 answer

0

I came to this solution, I’m not sure if it’s the right pooooorem....

I modified the two ROUTES:

        ///ROTAS PARA FABRICANTE
        routes.MapRoute(
                name: "EditarFabricante",
                url: "{controller}/" +
                     "{action}/" +
                     "{org_tab_in_codigo}/" +
                     "{org_pad_in_Codigo}/" +
                     "{org_in_codigo}/" +
                     "{org_tau_in_codigo}/" +
                     "{fab_in_codigo}/" +
                     "{alf_in_codigo}",
                defaults: new
                {
                    controller = "Fabricante",
                    action = "EditarRegistro"
                });


        routes.MapRoute(
                name: "ExcluirFabricante",
                url: "{controller}/" +
                     "{action}/" +
                     "{org_tab_in_codigo}/" +
                     "{org_pad_in_Codigo}/" +
                     "{org_in_codigo}/" +
                     "{org_tau_in_codigo}/" +
                     "{fab_in_codigo}/" +
                     "{alf_in_codigo}",
                defaults: new
                {
                    controller = "Fabricante",
                    action = "ExcluirRegistro"
                });

Before I was passing the parameters directly on ROUTE, removed the parameters keeping the URL normally. Now I do the route modification directly on controller.

[Route("Fabricante/Editarregistro/{org_tab_in_codigo}/{org_pad_in_Codigo}/{org_in_codigo}/{org_tau_in_codigo}/{fab_in_codigo}/{alf_in_codigo}")]

    [Route("Fabricante/EditarRegistro/{org_tab_in_codigo}/{org_pad_in_Codigo}/{org_in_codigo}/{org_tau_in_codigo}/{fab_in_codigo}/{alf_in_codigo}")]
public ActionResult EditarRegistro(int org_tab_in_codigo,
                                           int org_pad_in_Codigo,
                                           int org_in_codigo,
                                           string org_tau_in_codigo,
                                           int fab_in_codigo,
                                           int alf_in_codigo)
        {
            ViewBag.NotView = ViewData.vNotView;
            var appFabricante = new WebEstFabricanteAplicacao();
            var webFabricante = appFabricante.ListarUmRegistrodaOrganizacao(org_tab_in_codigo,
                                                                            org_pad_in_Codigo,
                                                                            org_in_codigo,
                                                                            org_tau_in_codigo,
                                                                            fab_in_codigo,
                                                                            alf_in_codigo);

            return View(webFabricante);
        }

So he accepted the ROUTES :

http://localhost:49609/Manufacturer/Editarregistro/53/1/4/G/3/13

http://localhost:49609/Manufacturer/Exclurregistro/53/1/4/G/3/13

And not getting lost and passing null value to integer parameter.

Thank you all.

Browser other questions tagged

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