简体   繁体   English

如何根据他的ID将超链接放置到另一个页面?

[英]How do I put an hyperlink to another page according with his ID?

I'm new to ASP.NET MVC and I'm stuck in here. 我是ASP.NET MVC的新手,但我一直呆在这里。 I want to create a hyperlink, so that when I click in the movie name, it goes to a details page, with the details on it. 我想创建一个超链接,以便当我单击电影名称时,它会转到一个包含详细信息的详细信息页面。 I already have the @HTML.ActionLink to a Details class in the controller, but now I don't know what to put in the Details class. 我已经有@ HTML.ActionLink到控制器中的Details类,但是现在我不知道在Details类中放什么。 Should I create a variable to the class Index to make comparisons? 是否应该为类Index创建变量以进行比较?

This is the action method Index : 这是操作方法Index

public ActionResult Index()
{
        PAPEntities db = new PAPEntities();

        MovieViewModel[] movies = db.MoviesData.Select(movie => new MovieViewModel
        {
            MovieID = movie.MovieID,
            MovieName = movie.MovieName,
            MovieDescription = movie.MovieDescription,
            MovieCategory = movie.MovieCategory,
            MovieYear = movie.MovieYear
        }).ToArray();

        return View(movies);
}

This is the part of the Index with the @HTML.ActionLink: 这是带有@ HTML.ActionLink的索引的一部分:

@foreach (var item in Model)
    {
        <tr>
            <td>@Html.ActionLink(item.MovieName, "Details", "Movies", new { id = item.MovieID }, null)</td>
            <td>@item.MovieCategory</td>
            <td>@item.MovieYear
            <td>@item.MoviePrice</td>
        </tr>
    }
</table>

I tried to search for it, but nothing similar appears, so I'm kinda stuck 我试图搜索它,但是没有类似的东西出现,所以我有点卡住了

I assume you're using asp.net core? 我假设您正在使用asp.net核心? if so you should be using Tag Helpers 如果是这样,您应该使用标签助手

Add the following in the _ViewImports.cshtml 在_ViewImports.cshtml中添加以下内容

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Then the following in your page 然后您页面中的以下内容

<a id="btn" asp-controller="Movies" asp-action="Details" asp-route-id="@item.MovieID">@item.MovieName</a>

I think you are confusing class and method . 我认为您在混淆classmethod
The code you posted is the method Index on the class (I guess) MoviesController . 您发布的代码是类MoviesController上的Index方法。
This line 这条线

<td>@Html.ActionLink(item.MovieName, "Details", "Movies", new { id = item.MovieID }, null)</td>

creates a link to the action Details in the controller Movies , passing an id to it. 在控制器Movies创建到操作Details的链接,并向其传递ID。

So you need in your class MoviesController the method Details having a parameter called id to get it and use it. 因此,您需要在MoviesController类中使用名为id的参数的Details方法来获取和使用它。

public class MoviesController: Controller
{
    // code of other methods

    public ActionResult Details(int id)
    {
        // get details of movie with id
    }
}

Side note: if your using ActionLink in a view returned by the Movies controller, you don't need to specify the controller again so you can write 旁注:如果您在Movies控制器返回的视图中使用ActionLink ,则无需再次指定控制器即可编写

@Html.ActionLink(item.MovieName, "Details", new { id = item.MovieID })

and it will do the same. 它将执行相同的操作。

If the @Html.ActionLink is meant to hit the Details method of the Movies controller, and you say that: 如果@Html.ActionLink旨在击中Movies控制器的Details方法,那么您说:

I already have the @HTML.ActionLink to a Details class in the controller, but now I dont know what to put in the Details class 我已经有@ HTML.ActionLink到控制器中的Details类,但是现在我不知道要在Details类中放入什么

For that I would suggest to add a method 'Details' like this in the 'Movies' controller: 为此,我建议在“电影”控制器中添加类似“详细信息”的方法:

public ActionResult Details(string id)   // might be 'int' if your MovieId is of integer type
{
    //use LINQ to fetch the movie based on input 'id'

    //return the information with the 'details' view as you did with the 'Index' method
}

Hope I have got your question correctly. 希望我能正确回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在现有PDF中使用iTextSharp插入到另一页的超链接? - How do I insert a hyperlink to another page with iTextSharp in an existing PDF? 如何将文章ID从网址传递到另一个页面? - How do I pass the post id from the url to another page? 如何在 Razor 页面上创建指向存储在 wwwroot 中的图像的超链接? - How do I create a hyperlink on a Razor page to an image stored in wwwroot? 如何根据当前显示的页面更改HyperlinkBut​​tons的图像 - How do I Change HyperlinkButtons' image according to the page that is currently displayed 如何根据表格元素及其ID设置模态对话框的标题文本 - How to Set Header Text of Modal Dialog According to the Table element and his ID 如何使用gridview(一个新的弹出窗口)中的超链接字段链接回我的主页 - How do I use the hyperlink field in gridview, a new pop-up, to link back to my main page 如果页面在另一个文件夹中,如何将页面放入 tabItem? - How to put page in tabItem, if the page is in another folder? 如何将命名空间放在C#.aspx页中? - How do I put the namespace in a C#.aspx page? 如何更新另一个MVC页面? - How do I update another MVC page? 我如何知道TextRange是超链接还是包含超链接? - How do I know if a TextRange is or contains a Hyperlink?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM