Url with parameters does not work in MVC

1

I am developing a project in mvc it has been working fine, but I have not been able to solve an error, I try to access a url that creates from global asax with parameters but it sends me error 404

 routes.MapRoute(
            "Unit",
            "Home/Unit/{id}/{name}",
            new { controller = "Home", action = "Unit", id = UrlParameter.Optional }
            );

here I create the route in jquery, I add the path to a div with text

 $('#nombreUnidad').append("<a href='Unit/" + param.Unidad.Id + "/" + param.Unidad.NombreUnidad + "'>Nombre: " + param.Unidad.NombreUnidad + "</a>");

so is my actionresult from my controller

 public ActionResult Unit(int id, string name)
    {

        return View("~/Views/Home/Unit.cshtml");
    }

the other routes that only have views and do not have parameters if they work

    
asked by Josue Leonardo Galindo Miranda 06.04.2018 в 03:24
source

2 answers

0

In MVC 5 to return a view, it is not necessary to load the route when the controller has the same view / partial name.

public ActionResult Unit(int id, string name)
{
    return View();
}

Now, if in your view you have defined some model or variable, there you will have to assign it to the view ();

public ActionResult Unit(int id, string name)
{
    var model= bd.where(w => w.id == id && w.name == name)
    return View(model);
}
    
answered by 06.04.2018 в 03:45
0

Try placing the full path in the div, like this:

 $('#nombreUnidad').append("<a href='http://localhost:puerto/Unit/" + param.Unidad.Id + "/" + param.Unidad.NombreUnidad + "'>Nombre: " + param.Unidad.NombreUnidad + "</a>");

You can also try to make a request to the action with fiddler or postman and place a breakpoint in the action to verify that it makes the call to the controller.

    
answered by 06.04.2018 в 19:30