2

My current code in Index.cshtml page:

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

The above code makes me to open a page(Name.cshtml) in a new tab after clicking a link in the Index page. But my goal is to assign a name(objList.Name) as a Title for the new tab. So, I added the Title attribute in the following code but it is not working as expected.

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

How to acheive this?

4

3 回答 3

2

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

ViewBag.MyTitle = nameParameter;

在“名称”视图中:

<title>@ViewBag.MyTitle</title>

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

ViewBag.Title = nameParameter;

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

<title>@ViewBag.Title</title>
于 2013-04-18T07:19:37.887 回答
0

只需将其添加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

然后ViewBag.TitleName视图中设置:

ViewBag.Title = Model.Name;
于 2013-04-18T08:40:33.883 回答
0

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

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

于 2013-04-18T08:15:10.010 回答