简体   繁体   中英

Razor/Mvc4 web page bad redirection of Html.ActionLink

Well i'm new here, i'll try to be clear.

I'm making a forum with Razor/Mvc4, the problem is when i try to use this:

 @Html.ActionLink(@elemento.nombre, "Temas", "Categorias", new { id = @i, onclick = 
 "javascript:IdCat(this)" })

I don't know why but when i press the link it redirects me to Home/Temas and i want to go Categorias/Tema.

In my project i have two controllers, HomeController and CategoriasController, i think everything is ok, but it keeps redirecting to Home/Temas.

public class CategoriasController : Controller
{
    public ActionResult Temas()
    {
        Session["user_name"] = Session["user_name"];
        Session["IDG"] = Session["IDG"];
        Session["ID"] = Session["ID"];

        Tema tem = new Tema();
        List<Tema> temas = new List<Tema>(); 
        temas = tem.ObtenerTemasPorCategoriaID(int.Parse(Session["idCat"].ToString()));

        Categoria cat = new Categoria();
        ViewBag.NombreCat = cat.obtenerNombreCategoriaById(int.Parse(Session["idCat"].ToString()));

        return View();
    }

}

Hopefully someone can help. Many thanks.

Well, for one your Razor syntax is incorrect. You should NOT include the @ escape prefix when referencing variables in the @Html.ActionLink() helper call:

 @Html.ActionLink(elemento.nombre, "Temas", "Categorias", new { id = i, onclick = 
 "javascript:IdCat(this)" })

The only time an @ would be valid is if you were escaping a keyword, as in @class = someClass .

Also, you should realize that the onclick call may override your link. The onclick function will execute prior to following the hyperlink. The link will be followed only if the function returns true If the function returns false , the link will not be followed!

The overload of the Html.ActionLink that you are using is incorrect for a couple of reasons.

First, the onclick is not a RouteValueParameter and actually should be in the HtmlAttributes portion of the Html.ActionLink . Second, I think the overload you are actually looking for is (note the controller will be moved to the Object routevalues parameter):

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

With all this said, the correct syntax should be:

 @Html.ActionLink(elemento.nombre, "Temas", new { controller="Categorias", nid = i}, new {onclick = = "javascript:IdCat(this)" })

If you are still having issues, I think you should consider examining your route definitions to ensure you are not routing incorrectly. Also, the javascript could be causing an issue as well and without seeing the JavaScript code, it may be hard to say. I see above that you are setting a Session variable with JavaScript (which won't work because one is client side and one is server side - and Session is completely server side). You should be able to set the session in the "Temas" ActionResult without having to use JavaScript.

A full list of the available Html.ActionLink() overloads are available here . EDIT

You are sending the Id, but your ActionResult method isn't looking for it. Change your ActionResult in your controller to the following(note that we have added the nid you are passing from the ActionLink to the parameters of the function):

public ActionResult Temas(int nid)
    {
    Session["user_name"] = Session["user_name"];
    Session["IDG"] = Session["IDG"];
    Session["ID"] = Session["ID"];

    Tema tem = new Tema();
    List<Tema> temas = new List<Tema>(); 
    temas = tem.ObtenerTemasPorCategoriaID(nid);

    Categoria cat = new Categoria();
    ViewBag.NombreCat = cat.obtenerNombreCategoriaById(nid);

    return View();
}

try this

       @Html.ActionLink(@elemento.nombre, "Temas", "Categorias",  new { id = @i} ,new{onclick = "javascript:IdCat(this)" })

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM