RouteData 中的参数正确,但在控制器中作为 null 传递
我现在使用 MVC 已经几周了,每天都会出现一些新的东西,这让我觉得很奇怪。因此,我尝试寻找我面临的问题的答案。尽管如此,对于当前的问题,我似乎无法在 stackoverflow 上找到下降答案,或者在 google 上的任何地方都找不到这个问题...
我在使用 HTML.RenderAction 方法将参数传递到我的控制器时遇到问题。由于某种原因,参数最终正确地出现在 RouteData 中,但“函数参数”为空。我认为这与我的路由映射有关,所以我想将它们发布在这里以获取更多输入。
我的路线图(除其他外,但我知道我当前的操作正在使用此根):
routes.MapRoute("Default","{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" },
null,
new[] { "Web.Controllers" }
);
我的控制器操作:
public ActionResult GeneralManagementDetail(int? id)
{
//dostuff
}
RenderAction 调用:
<% Html.RenderAction("GeneralManagementDetail", "Person", new { id = 4 }); %>
当然,“4”当前是一个硬编码值,它将成为来自包含我在那里的 foreach 循环。
现在,这会导致控制器中的“int id”为NULL,但是当“快速监视”RouteData时,它肯定有一个键值对“id,4”......
I'm a few weeks into MVC now, and each day, something new pops up which strikes me as quite odd. So, I try to find answers to the issues I'm facing. None the less, for the current issue, I can't seem to find a descent answer here on stackoverflow, or anywhere on google for that matter...
I'm having an issue passing parameters to my controller using the HTML.RenderAction method. For some reason, the parameter ends up correctly in the RouteData, but the "function parameter" is null. I think it has to do with my routing maps, so I'd like to post them here for some more input.
My routemap (amongst others, but I know my current action is using this root):
routes.MapRoute("Default","{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" },
null,
new[] { "Web.Controllers" }
);
My controller action:
public ActionResult GeneralManagementDetail(int? id)
{
//dostuff
}
The RenderAction call:
<% Html.RenderAction("GeneralManagementDetail", "Person", new { id = 4 }); %>
Where of course the "4" currently is a hardcoded value, and it will become an id of an object from the containing foreach loop I have there.
Now, what this results into, is that the "int id" in the controller is NULL, however when "QuickWatching" the RouteData, it definitely has a keyvalue pair "id,4" in it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,只是为了澄清一下,问题显然是控制器被缓存在提供给我们的框架中(不同的开发团队负责框架)。因此,当我们深入研究框架并确保容器不再被缓存时,参数的传递就像一个魅力。
非常感谢您的回答,但问题似乎是我们自己公司内部的!
Ok, just to clarify, the issue apparently was that the controllers were being cached in the framework provided to us (different dev-team does the framework). So, when we dug into the framework, and made sure that the containers weren't cached anymore, the passing of the parameters worked like a charm.
Thanks a lot for the answers, but the issue was within our own company it seems!
也许“id = (int?)null”可以解决问题?我的意思是,如果类型搞砸了怎么办?
Maybe "id = (int?)null" will do the trick? I mean, what if types are messed up.