How to set a name to the TITLE of a newly opened t

2019-07-17 02:09发布

问题:

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?

回答1:

You have to pass the objList.Name as a parameter to the "Name" action, in this action you can include the name in the Viewbag:

ViewBag.MyTitle = nameParameter;

And in the "Name" view:

<title>@ViewBag.MyTitle</title>

If you are using a Layout page, you only have to put this in the "name" action:

ViewBag.Title = nameParameter;

Because in your layout view you probably have this:

<title>@ViewBag.Title</title>


回答2:

The 'title' in your code is adding a mouseover title on the link, I think.

To have the title in the tab, use the solution from carlos (put the title tag and viewbag prop in your viewpage).



回答3:

Just add the Name into the RouteValueDictionary, add it to your ActionResult and then set ViewBag.Title.

First, change your ActionLink to be:

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

Then create a model (if you don't have one on the Name table), something like:

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

Then change your Name ActionResult to be:

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

        return View(model);
    }

Then add the model to your Name view:

@model MyModel

Then set ViewBag.Title in the Name view:

ViewBag.Title = Model.Name;