ASP.Net MVC:如何将查询字符串参数传递给Child Actions?

时间:2012-04-23 20:04:17

标签: asp.net-mvc view controller query-string

我有一个调用一些子控制器动作的视图。其中一个子操作将检查可选的查询字符串值,这个特定的查询字符串将是一个整数,我想知道将这个值作为子操作中的参数是否谨慎?

我没有使用参数的原因是因为View必须将查询字符串解析为整数并执行空检查,因此子操作只是在内部检查是否存在查询字符串。 / p>

这是推荐的方法吗?或者在action方法中有一个参数更好(并且更可测试)?

由于

1 个答案:

答案 0 :(得分:1)

您只需使用Html.Action / Html.RenderAction并将您需要的数据专门传递给此操作方法:

 public ActionResult Sample(SomeViewModel model)
 {
     // Do Stuff
 }

然后在你看来:

 @Html.Action("Sample", new { model =  new SomeViewModel { Property = "HelloWorld" } });