C# 如何为@Html.ActionLink cshtml页面中新打开的选项卡的标题设置名称
我在C# 如何为@Html.ActionLink cshtml页面中新打开的选项卡的标题设置名称,c#,asp.net-mvc,asp.net-mvc-3,razor,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,Asp.net Mvc 4,我在Index.cshtml页面中的当前代码: @Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id}, new {target = "_blank"})) 上面的代码使我在单击索引页面中的链接后,在新选项卡中打开一个页面(Name.cshtml)。但我的目标是为新选项卡指定一个名称(objList.name)作为标题。 因此,我在下面的代码中添加了Title属
Index.cshtml
页面中的当前代码:
@Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id}, new {target = "_blank"}))
上面的代码使我在单击索引
页面中的链接后,在新选项卡中打开一个页面(Name.cshtml
)。但我的目标是为新选项卡指定一个名称(objList.name
)作为标题。
因此,我在下面的代码中添加了Title属性,但它没有按预期工作
@Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id}, new {target = "_blank", title = objList.Name}))
如何实现这一点?您必须将objList.Name作为参数传递给“Name”操作,在此操作中,您可以将名称包含在Viewbag中:
ViewBag.MyTitle = nameParameter;
在“名称”视图中:
因为在布局视图中,您可能有:
<title>@ViewBag.Title</title>
@ViewBag.Title
我认为,您代码中的“标题”是在链接上添加鼠标标题
要在选项卡中显示标题,请使用carlos提供的解决方案(将标题标签和viewbag道具放在您的viewpage中)。只需将名称添加到RouteValueDictionary
,将其添加到操作结果中,然后设置viewbag.title
首先,将您的ActionLink
更改为:
Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id, Name=objList.Name}, new {target = "_blank"}))
public ActionResult Name(int Id, string Name)
{
MyModel model = new MyModel
{
Id = Id,
Name = Name
};
return View(model);
}
然后创建一个模型(如果名称表中没有),类似于:
public class MyModel
{
public int Id { get; set; }
public string Name { get; set; }
}
然后将您的名称
操作结果
更改为:
Html.ActionLinks(@Html.ActionLink(objList.Name, "Name", "ControllerName", new {Id=objList.Id, Name=objList.Name}, new {target = "_blank"}))
public ActionResult Name(int Id, string Name)
{
MyModel model = new MyModel
{
Id = Id,
Name = Name
};
return View(model);
}
然后将模型添加到名称视图中:
@model MyModel
ViewBag.Title = Model.Name;
然后在Name
视图中设置ViewBag.Title
:
@model MyModel
ViewBag.Title = Model.Name;
在you name.cshtml标记中-我的标题没有提到标记,如。。在name.cshtml
pageYes-将该标记添加到您的cshtml页面-您不需要任何额外的代码在操作链接中正常工作。谢谢