ASP.NET MVC - Route does not pass values to the Controller

Asked

Viewed 1,465 times

0

I created a new Controller with name Conteudo. Then I created an Action on it called conteudo and added a new View called Conteudo. Before it was PaginaBase and the Controller of Home. Well, I copied the contents of the old View(PaginaBase) for this new View(Conteudo) and when I give a View in browser, it gives Page not found(404) error. In the Url it looks like this: .. /Content/Content.

My route is like this:

routes.MapRoute(
   name: "RotaConteudo",
   url: "Conteudo/{Parametro}/{tipo}",
   defaults: new { controller = "Conteudo", action = "Conteudo", Parametro = "", tipo = "" }
);

Okay, here’s the deal. I created a new view and a new controller. Inside the controller, I created an action(Actionresult). I created a new view and pointed to this controller. This Action, receives two parameters, as I did in Route. What happens is that nothing is working. There is no error, but it also does not work. Trying to solve now, I changed the parameters from int to string, I did view in browser and this way I was able to view the view. But not by the app. I put a break in Action and it doesn’t stop. It doesn’t do anything and it doesn’t work. In other words, my controller isn’t working.

My action:

public ActionResult Conteudo(string nome, int Parametro, int tipo)
{
    AgaxturCmsEntities db = new AgaxturCmsEntities();

    int _parametro = Parametro;

    try
    {
        switch (tipo)
        {
            case 1:
                {
                    var resultado = (from i in db.TB_INSTITUCIONAL
                                     join c in db.TB_INSTITUCIONAL_CATEGORIAS on i.Id_Categoria equals (c.id)
                                     where i.Ativo == 1 && c.Ativo == 1 && c.CdCliente == 1 && i.Id_Categoria == _parametro
                                     select new
                                     {
                                         Conteudo = i.Conteudo
                                     }).FirstOrDefault();


                    ViewData["htmlBase"] = resultado.Conteudo;
                    break;
                }
            case 2:
                {

                    var menu_sup = (from rc in db.TB_MENUSUPERIOR
                                    join c in db.TB_MENUSUPERIOR_CATEGORIAS on rc.Id_Categoria equals (c.id)
                                    //join s in db.TB_MENUSUPERIOR_SUBCATEGORIAS on rc.Id_SubCategoria equals (s.id)
                                    //join s2 in db.TB_MENUSUPERIOR_SUBCATEGORIAS2 on rc.Id_SubCategoria2 equals (s2.id)
                                    where rc.Ativo == 1 && rc.Cdcliente == 1 && rc.Id_Categoria == _parametro
                                    select new
                                    {
                                        Conteudo = rc.Conteudo

                                    }).FirstOrDefault();

                    ViewData["htmlBase"] = menu_sup.Conteudo;
                    break;
                }
            case 3:
                {
                    var menu_sup = (from rc in db.TB_MENUSUPERIOR
                                    join s in db.TB_MENUSUPERIOR_SUBCATEGORIAS on rc.Id_SubCategoria equals (s.id)
                                    join s2 in db.TB_MENUSUPERIOR_SUBCATEGORIAS2 on rc.Id_SubCategoria2 equals (s2.id)
                                    where rc.Ativo == 1 && rc.Cdcliente == 1 && rc.Id_SubCategoria2 == _parametro
                                    select new
                                    {
                                        Conteudo = rc.Conteudo

                                    }).FirstOrDefault();

                    ViewData["htmlBase"] = menu_sup.Conteudo;
                    break;
                }
        }
        return View("Conteudo");
    }
    catch (Exception ex)
    {
        string e = ex.Message;
    }
    return View();
}

My jquery

function MontaMenuInferior() {

    var str = "";
    $.ajax({
        url: '/Conteudo/MontaMenuInferior',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        type: "POST",
        success: function (data) {

            $(data.resultado).each(function () {

                str = str + '<ul class="grid_4">' +
                                    '<li>' + this.SubCategoria + '</li>';


                $(this.subconsulta).each(function () {

                    if (this.Id_SubCategoria2 != null)

                        str = str + '<li><a href="/Conteudo/' + 'teste-de-nome/' + this.Id_SubCategoria2 + '/3" title="">' + this.SubCategoria2 + '</a></li>';
                        //str = str + '<li><a href="@Url.RouteUrl("PaginaBase",new{ Parametro = 6, tipo = 3} )">' + this.SubCategoria2 + '</a></li>'
                    else
                        str = str + '<li><a href="#' + this.SubCategoria2 + '" title="">' + this.SubCategoria2 + '</a></li>';

                });

                str = str + '</ul>';

                $('#menufooter').append(str);

                str = "";

            });
        },
        error: function (error) {

        }
    });
}
  • 1

    Your question is very confusing, you can improve it?

  • Okay, here’s the deal. I created a new view and a new controller. Inside the controller, I created an action(Actionresult). I created a new view and pointed to this controller. This Action, receives two parameters, as I did in Route. What happens is that nothing is working. There is no error, but it also does not work. Trying to solve now, I changed the parameters from int to string, I did view in browser and this way I was able to view the view. But not by the app. I put a break in Action and it doesn’t stop. It doesn’t do anything and it doesn’t work.

  • Update on your question.

  • I posted my action and my js function, to see if there is anything missing. In my cshtml(View), I pointed to js Conteudo.js.

  • I think my problem is this, after thinking a lot. I have an Index that assembles the menu. In this menu I must call the Content, passing the proper parameters, to get the correct HTML of the BD. In the index I also point to Conteudo.js. I’m not able to move the menu up by passing the route. I put a break in both Controller and . js and can’t debug, not break. I can’t be too clear in my doubt, for lack of understanding of the whole thing.

  • What did you mean by the route you would take, in this part ? { parametro = "", type = "" }

Show 1 more comment

2 answers

2

The option View in browser... from Visual Studio, it’s kind of incompatible with ASP.NET MVC, because it tries to navigate to the page with a URL using the folder structure as a template, rather than using the route to the page that was set via code.

To view the page correctly, you must run the application and place the desired URL in the browser, without waiting for Visual Studio to do it for you.

  • This I do. And worse, the app doesn’t work, as I said before. It doesn’t. Now, if I create in the Home folder, then it works, but I need to create different folders to better organize.

  • Did you change the class name? I mean, from Homecontroller to Conteudocontroller?

  • I created another one. The home continues and I made another one for Conteudo. That’s when the thing doesn’t go. In this structure. Folder Controller has two controllers. Home and Content. In folder View has two folders. One called Home, with the Index view and another named Content with the content view. The Content Controller, which has the methods to load the Menu and mount the URL of the new page, does not go up.

  • Try to isolate the problem and post the isolated problem code. In your example there are a lot of calls to the database, and this has no relation to the problem, so it is possible to reproduce the problem... so it would be easier to help.

  • Well, in order for me to have a composite URL with the name Content, I need to have a Controller called content and of course, within a Call content folder. It turns out, I created this control and pointed to a view of the same name. I just can’t make it work. It doesn’t work. If I put in Home works, but if I put in Content does not work. How works the site. The index, goes up and mounts a menu, by the way two, top and bottom. Continues in the next comment

  • When mounting this menu, there are submenus that point to a dynamic link. This is the link I need to set up like this: www.meudominio.com.br/Content/page-name-to-the-goofgle-find/parametro2/parametro3(The page name is a parameter too). That’s exactly what I can’t do. That’s the route to all of this.

Show 1 more comment

0

I don’t understand what you wanted to do in this part !

defaults: new { controller = "Conteudo", action = "Conteudo", Parametro = "", tipo = " }

Try something like that:

{ controller = "Conteudo", action = "Conteudo", Parametro = UrlParameter.Optional, tipo = UrlParameter.Optional }

Browser other questions tagged

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