ASP.NET MVC - 将参数传递给我的操作方法?
public ActionResult RenderMyThing(IList<String> strings)
{
return View("RenderMyView");
}
我如何传入字符串?
routes.MapRoute("MyRoute", "RenderMyThing.aspx", new { controller = "My", action = "RenderMyThing" });
有没有办法可以在这里传递字符串?
其次,ASP.NET MVC如何知道action是我的action,controller是我的控制器。就像我在示例中看到的那样,它确实有效,但它不只是一个没有类型的匿名对象吗?
public ActionResult RenderMyThing(IList<String> strings)
{
return View("RenderMyView");
}
How do I pass in strings?
routes.MapRoute("MyRoute", "RenderMyThing.aspx", new { controller = "My", action = "RenderMyThing" });
Is there a way I could pass in strings here?
Secondly, how does ASP.NET MVC know that action is my action, and controller is my controller. Like I saw this in samples, and it does work, but isn't it just an anonymous object with no type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是模型绑定的起源:框架需要一些指令来说明如何将来自路由上下文、查询字符串、表单集合等的“请求”转换为操作方法所需的参数。
如果
DefaultModelBinder
发现您有多个具有相同键(以及适当类型/可转换值)的键值对,它将生成一个列表 - 有关详细信息,Phil 写了一篇关于此的好文章:如果您需要更高级的绑定要求,您可以可以实现自定义模型绑定器并显式定义如何将路由值和其他位转换为对象(或对象集合)。
This is the provenance of model binding: the framework needs to have some instruction as to how to turn a "request", which comes out of the routing context, query string, forms collection, etc., into the parameters that your action method wants.
The
DefaultModelBinder
will generate a list if it sees that you have multiple key-value pairs with the same key (and appropriately typed/convertible values) - for the details, Phil wrote a good post about this:If you need fancier binding requirements, you can implement a custom model binder and explicitly define how route values and the other bits get translated into objects (or collections of objects).