通过url MVC 3将变量发送到控制器

时间:2012-10-29 00:19:36

标签: asp.net-mvc url razor

我需要这个网址“Home / Details / 123”显示一个页面详细信息,其中包含ID为123的项目的详细信息,我该怎么办?我认为该路线必须注册,但我需要更多相关信息。

由于

1 个答案:

答案 0 :(得分:0)

在您的Home控制器中,创建一个名称为Details的操作方法,该方法的int参数名称为id

public class HomeController: Controller
{
   public ActionResult Details(int id)
   {
     //get the item from the id and return the view
     Customer customerModel=repositary.GetCustomerFromId(id);
     return View(customerModel);
   }
}

假设repositary.GetCustomerFromId方法返回Customer类的对象,并且您的详细信息视图强类型为Customer类。

@model Customer
<h2>@Model.FirstName</h2>
<p>@Model.AddressLine1</p>

您在global.asax中的默认路由定义就足够了。

相关问题