[英]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
. 我认为您在混淆class
和method
。
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.