简体   繁体   English

将ID传递给URL,ASP.NET MVC

[英]Passing ID to URL, ASP.NET MVC

I am trying to pass an id to the URL, However I cant hard code the ID but I need to pass the id from the model. 我正在尝试将ID传递给URL,但是我无法对ID进行硬编码,但是我需要从模型传递ID。

It is basically a collection of a particular type of model displayed as a table. 它基本上是显示为表格的特定类型模型的集合。 When the user clicks on the id value of the table i need to be able to pass the id to the URL so that it can open a corresponding detailed page of the same. 当用户单击表的ID值时,我需要能够将ID传递给URL,以便它可以打开该表的相应详细页面。

<a href="~/Instance/Details/id=@Url.Encode(modelitem => item.Id)">   @Html.DisplayFor(modelitem => item.Id)</a>

This is what I am trying, need help:). 这是我正在尝试的,需要帮助:)。

First of all, use the Url.Action method to generate the correct relative url to the action method. 首先,使用Url.Action方法生成正确的相对URL到action方法。

@foreach (var item in SomeCollection)
{
  <div>
       <a href="@Url.Action("Details","Instance",new { id = item.Id })"> 
                                            @Html.DisplayFor(modelitem => item.Id)
       </a>
  </div>
}

The Url.Action method will generate the url value to details action method, which includes the item.Id value in the url. Url.Action方法将生成用于详细说明操作方法的url值,其中包括url中的item.Id值。

Assuming Instance is the controller name where your Details action method is present with id parameter. 假设Instance是控制器名称,其中您的Details动作方法带有id参数。

public class InstanceController : Controller
{
   public ActionResult Details(int id)
   {
      return Content("Details of "+id);
      // to do : return a view with data queried using id
   }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM