将params添加到现有的ActionResult对象

时间:2014-02-19 18:54:27

标签: c# asp.net-mvc

我有一个方法GenerateLinks(),它接受​​一个名为ActionResult的{​​{1}}对象。在该方法中,我想使用result的修改版本生成链接,该版本将包含每个result的附加ID参数。请注意,id需要HtmlHelper.ActionLink()

ActionResult

即使我知道除了使用// Client-side @Html.GenerateLinks(Mvc.Areas.MyArea.Index(), Model.ids); // Server-side MvcHtmlString GenerateLinks(this HtmlHelper helper, ActionResult result, List<string> ids) { var sb = new StringBuilder(); foreach (var id in ids) { // How to add id to result where result becomes Mvc.Areas.MyArea.Index(id)? sb.Append(helper.ActionLink(id, result-with-id-as-param); sb.Append("<br/>"); } return new MvcHtmlString(sb.ToString()); } 之外还有其他方法来生成这些链接,我的最终问题是:是否可以将ActionLink作为参数添加到现有{{1}对象?如果是这样,怎么样?

感谢。

2 个答案:

答案 0 :(得分:0)

修改你的行动结果

public ActionResult Index(id = defaultvalue){
    //AR code here
}

然后根据需要传递ID。如果没有任何内容通过,你还可以为id设置defaultvalue

答案 1 :(得分:0)

找到答案:

foreach (var id in ids)
{
    // How to add id to result where result becomes Mvc.Areas.MyArea.Index(id)?

    // *** ANSWER
    result.GetRouteValueDictionary()["id"] = id;
    // *** ANSWER

    sb.Append(helper.ActionLink(id, result);
    sb.Append("<br/>");
}