【问题标题】:How to set a name to the TITLE of a newly opened tab from the @Html.ActionLink cshtml page如何从@Html.ActionLink cshtml 页面为新打开的选项卡的 TITLE 设置名称
【发布时间】:2013-04-18 07:08:53
【问题描述】:

我当前在Index.cshtml页面中的代码:

@Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id}, new {target = "_blank"}))

上面的代码使我在单击Index 页面中的链接后在新选项卡中打开一个页面(Name.cshtml)。但我的目标是为新标签分配一个名称(objList.Name)作为Title。 所以,我在下面的代码中添加了 Title 属性,但它没有按预期工作。

 @Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id}, new {target = "_blank", title = objList.Name}))

如何做到这一点?

【问题讨论】:

  • In you name.cshtml 标记 - 我的标题
  • Thers 在name.cshtml 页面中没有提及为 .. 的标记
  • 是的 - 将该标记添加到您的 cshtml 页面 - 您不需要在 Action Link 中添加任何额外代码

标签: c# asp.net-mvc asp.net-mvc-3 razor asp.net-mvc-4


【解决方案1】:

您必须将 objList.Name 作为参数传递给“名称”操作,在此操作中您可以将名称包含在 Viewbag 中:

ViewBag.MyTitle = nameParameter;

在“名称”视图中:

<title>@ViewBag.MyTitle</title>

如果您使用的是布局页面,您只需将其放入“名称”操作中:

ViewBag.Title = nameParameter;

因为在你的布局视图中你可能有这个:

<title>@ViewBag.Title</title>

【讨论】:

  • 按预期工作正常。谢谢。
【解决方案2】:

我认为,您代码中的“标题”是在链接上添加鼠标悬停标题。

要在标签中添加标题,请使用 carlos 的解决方案(将标题标签和 viewbag 属性放在您的视图页面中)。

【讨论】:

    【解决方案3】:

    只需将Name 添加到RouteValueDictionary,将其添加到您的ActionResult,然后设置ViewBag.Title

    首先,将您的 ActionLink 更改为:

    Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id, Name=objList.Name}, new {target = "_blank"}))
    

    然后创建一个模型(如果 Name 表上没有模型),类似于:

    public class MyModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    然后将您的 Name ActionResult 更改为:

        public ActionResult Name(int Id, string Name)
        {
            MyModel model = new MyModel 
            {
                Id = Id,
                Name = Name
            };
    
            return View(model);
        }
    

    然后将模型添加到您的 Name 视图中:

    @model MyModel
    

    然后在Name 视图中设置ViewBag.Title

    ViewBag.Title = Model.Name;
    

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 2015-07-26
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2015-11-14
      相关资源
      最近更新 更多