如果我有这样的动作:
public ActionResult DoStuff(List<string> stuff)
{
...
ViewData["stuff"] = stuff;
...
return View();
}
Run Code Online (Sandbox Code Playgroud)
我可以使用以下URL点击它:
http://mymvcapp.com/controller/DoStuff?stuff=hello&stuff=world&stuff=foo&stuff=bar
Run Code Online (Sandbox Code Playgroud)
但在我的ViewPage中,我有这个代码:
<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData["stuff"] }, null) %>
Run Code Online (Sandbox Code Playgroud)
不幸的是,MVC不够聪明,无法识别该动作采用数组,并展开列表以形成正确的URL路由.相反,它只是在对象上执行.ToString(),它只列出了List中的数据类型.
当目标Action的参数之一是数组或列表时,有没有办法让Html.ActionLink生成正确的URL?
- 编辑 -
正如Josh在下面指出的那样,ViewData ["stuff"]只是一个对象.我试图简化问题,但引起了一个无关的错误!我实际上使用的是专用的ViewPage <T>,因此我有一个紧密耦合的类型感知模型.ActionLink实际上看起来像:
<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData.Model.Stuff }, null) %>
Run Code Online (Sandbox Code Playgroud)
其中ViewData.Model.Stuff被键入为List
我的视图如下所示:
<%@ Control Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<TMS.MVC.BusinessSystemsSupport.Models.SearchDataTypeModel>" %>
<table class="classQueryResultsTable">
<!-- the header -->
<tr class="headerRow">
<td>
<%= Html.ActionLink("Effective Startdate",
"SortDetails",
"DataQryUpdate",
new
{
model = Model,
sortBy = "EffectiveStartDate",
},
new { @class = "classLinkLogDetails" })%>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我的控制器动作:
public ActionResult SortDetails(SearchDataTypeModel model, String sortBy)
{
Run Code Online (Sandbox Code Playgroud)
model参数为null.sortBy参数已填充.我可以将模型中的String属性传递给操作,没有任何问题.我想传递整个模型.
我有什么想法我做错了吗?