简体   繁体   English

Html.ActionLink的Razor / Mvc4网页错误重定向

[英]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: 我正在用Razor / Mvc4建立论坛,问题是当我尝试使用此功能时:

 @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. 我不知道为什么,但是当我按下链接时,它会将我重定向到“首页/淡马锡”,我想去Categorias / Tema。

In my project i have two controllers, HomeController and CategoriasController, i think everything is ok, but it keeps redirecting to Home/Temas. 在我的项目中,我有两个控制器,HomeController和CategoriasController,我认为一切正常,但它一直重定向到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. 好吧,其中之一是您的Razor语法不正确。 You should NOT include the @ escape prefix when referencing variables in the @Html.ActionLink() helper call: @Html.ActionLink()帮助程序调用中引用变量时,不应包含@转义前缀:

 @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 . @唯一有效的时间是您是否转义了关键字,例如@class = someClass

Also, you should realize that the onclick call may override your link. 另外,您应该意识到onclick调用可能会覆盖您的链接。 The onclick function will execute prior to following the hyperlink. onclick函数将跟随超链接之前执行。 The link will be followed only if the function returns true If the function returns false , the link will not be followed! 仅当函数返回true才跟踪链接。如果函数返回false则不跟踪链接!

The overload of the Html.ActionLink that you are using is incorrect for a couple of reasons. 您使用的Html.ActionLink的重载是不正确的,原因有两个。

First, the onclick is not a RouteValueParameter and actually should be in the HtmlAttributes portion of the Html.ActionLink . 首先,onclick不是RouteValueParameter,实际上应该在Html.ActionLink的HtmlAttributes部分中。 Second, I think the overload you are actually looking for is (note the controller will be moved to the Object routevalues parameter): 其次,我认为您实际上正在寻找的重载是(请注意,控制器将被移至Object routevalues参数):

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. 另外,JavaScript也可能会引起问题,并且可能看不到JavaScript代码,这可能很难说。 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). 我在上面看到您正在使用JavaScript设置一个Session变量(这将不起作用,因为一个是客户端,一个是服务器端-而Session完全是服务器端)。 You should be able to set the session in the "Temas" ActionResult without having to use JavaScript. 您应该能够在“ Temas” ActionResult中设置会话,而不必使用JavaScript。

A full list of the available Html.ActionLink() overloads are available here . 此处提供了可用的Html.ActionLink()重载的完整列表。 EDIT 编辑

You are sending the Id, but your ActionResult method isn't looking for it. 您正在发送ID,但是您的ActionResult方法没有在寻找它。 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): 将控制器中的ActionResult更改为以下内容(请注意,我们已经将您从ActionLink传递的nid添加到了函数的参数中):

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 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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