在服务器上创建RouteValueDictionary并在aspx中使用?

时间:2011-11-28 23:40:51

标签: ajax asp.net-mvc-2 ajax.beginform

我想将RouteValueDictionary传递给我的aspx,以便我可以将它用作Ajax.BeginForm方法的参数。我加载它是这样的:

 RouteValues = new System.Web.Routing.RouteValueDictionary();

 RouteValues.Add("FindingId", thisFinding.Id);
 RouteValues.Add("ReportId", thisFinding.ReportSection.ReportId);

然后将其添加到我的模型中而不会出现问题。当我将它作为参数添加到BeginForm方法时,它会将操作呈现为:

/SolidWaste/Finding/LoadSection?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D

这是aspx代码:

(Ajax.BeginForm(Model.FormModel.Action,
    Model.FormModel.Controller, 
    Model.FormModel.RouteValues,
new AjaxOptions {
    HttpMethod = "Post",
    InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
    UpdateTargetId = "WindowContent",
}, new { id = FormId })) { %>
<input name="submit" type="submit" class="button" value="" style="float: right;"/>
<%  } //End Form %>

以下是表示Model.FormModel

的视图模型
    public class FormViewModel {

    public string Action { get; set; }

    public string Controller { get; set; }

    public string Method { get; set; }

    public RouteValueDictionary RouteValues { get; set; }
}

知道为什么它没有将RouteValueDictionary序列化为动作的正确URL吗?我想在这里使用一个对象,而不是用new { field = vale }

手工构建RouteValues

1 个答案:

答案 0 :(得分:3)

啊,你正在使用错误的重载。这是正常的。 ASP.NET MVC团队真的搞砸了这个API。你要小心你要调用哪种方法。这是您需要的the overload

<% using (Ajax.BeginForm(
    Model.FormModel.Action,                                // actionName
    Model.FormModel.Controller,                            // controllerName
    Model.FormModel.RouteValues,                           // routeValues
    new AjaxOptions {                                      // ajaxOptions
        HttpMethod = "Post",
        InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
        UpdateTargetId = "WindowContent",
    }, 
    new Dictionary<string, object> { { "id", FormId } })    // htmlAttributes
) { %>
    <input name="submit" type="submit" class="button" value="" style="float: right;"/>
<% } %>

注意正确的过载?您正在使用将routeValueshtmlAttributes作为匿名对象的那个,除了您将Model.FormModel.RouteValues作为RouteValueDictionary传递,这基本上废除了您的重载。

按住 F12 ,同时将光标悬停在BeginForm上,如果你足够幸运并且智能感知在Razor视图中很适合(很少发生),你将被重定向到方法你实际上正在调用并意识到你的错误。

相关问题