如何生成具有Name属性而不是Id的链接

本文关键字:Id 链接 属性 何生成 Name | 更新日期: 2023-09-27 17:57:38

我使用以下内容生成链接:

@Html.ActionLink(page, "Nouvelle", "Pages", new { Id = @item.Jeton }, null)

我的控制器:

public ActionResult Nouvelle(int Id)
        {
            var sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
            var partenaires = db.Partenaire.Where(p => p.Afficher);
            var nouvelle = db.Actualite.Where(a => a.Jeton == Id).First();
            var model = new ModelNouvelle { Slider = sliders, Partenaire = partenaires, Nouvelle = nouvelle };
            return View(model);
        }

链接结果如下:

http://something.org/Pages/Nouvelle/1

我想得到一个链接,它有名称而不是Id,比如:

http://something.org/Pages/Nouvelle/More-like-this

如何生成具有Name属性而不是Id的链接

我不明白为什么不。。你只需要更改你的路线属性。。并为您的操作更改方法签名。。

就像这样:

控制器:

public ActionResult Nouvelle(string routeValue)
    {
        var sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
        var partenaires = db.Partenaire.Where(p => p.Afficher);
        var nouvelle = db.Actualite.Where(a => a.Jeton /*this needs to be changed to the property that is doing the comparison*/ == routeValue).First();
        var model = new ModelNouvelle { Slider = sliders, Partenaire = partenaires, Nouvelle = nouvelle };
        return View(model);
    }

ActionLink:

@Html.ActionLink(page, "Nouvelle", "Pages", new { routeValue = @item.PropertyName /*whatever property holds the value that you want in your link*/ }, null)

如果这有帮助,请告诉我。

使用字符串作为参数修改操作。

public ActionResult Nouvelle(string _name)

在你的行动链接

@Html.ActionLink(page, "Nouvelle", "Pages", new { _name = "More-like-this" }, null)