[英]ASP.NET MVC Passing action parameter to the view
Let's say that I have a Action method like this: 假设我有一个这样的Action方法:
public ActionResult Page(int? id)
{
}
The question is, how can I read 'id' parameter in View? 问题是,如何在View中读取'id'参数?
Your code won't build successfully, until you return a view like this 您的代码将无法成功构建,直到您返回这样的视图
public ActionResult Page(int? id)
{
return View();
}
and because you want to return id to your view you can do Simple object 并且因为您想将id返回到视图,因此可以执行Simple对象
public ActionResult Page(int? id)
{
return View(id);
}
Just remember to accept the new value in your view, by dong the following at the top (very first line) 只需记住在您的视图中接受新值即可,方法是在顶部(非常第一行)添加以下内容
@model int?
ViewModel approach ViewModel方法
public class MyViewModel
{
public int? Id { get; set; }
//Other properties here
}
public ActionResult Page(int? id)
{
var myViewModel = new MyViewModel()
{
Id = id
};
return View(myViewModel);
}
and then in your view 然后在您看来
@model MyViewModel
您可以从“请求”视图中访问参数,如下所示
Request.Params["id"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.