好吧,asp.net mvc 与路由或 TableRoutes 一起使用。默认路由使用以下格式创建:{controller}/{action}/{id}。
因此,当您收到有关您的操作的请求时,您可以从您的操作(在控制器上)的 id 参数中检索此 ID,并使用此值访问您的数据库并获取您需要在看法。你可以试试这样的:
public ActionResult Recipes(string id)
{
IEnumerable<Recipe> list = _repository.GetRecipeByCookId(id); // this method should return list of Recipes
return View(list); // return your View called "Recipes" passing your list
}
您也可以使用Request.QueryString["Id"] 来获取Id,但这在asp.net mvc 中不是一个好习惯。您可以在操作中使用参数并使用它。
在您的视图中,您可以使用 IEnumerable<Recipe> 键入它并将其显示在表格上,例如:
@model IEnumerable<Recipe>
<table>
@foreach(var recipe in Model)
{
<tr>
<td>@recipe.Name</td>
<td>@recipe.CookId</td>
<td>@recipe.OtherProperties</td>
</tr>
}
</table>
要为请求创建一个传递此 ID 的链接,您可以使用 Html.ActionLink,类似于您的视图:
@Html.ActionLink("Text of You Link", "Action", "Controller", new { id = 5, another = 10 }, new { @class = "css class for you link" });
和asp.net mvc 将呈现a 标记,该标记具有遵循在global.asax 上设置的路由表的专用路由。如果您有其他参数要传入查询字符串,您也可以像我在示例中使用another 参数一样添加它。