1

a を aspx に渡してRouteValueDictionary、メソッドのパラメーターとして使用できるようにしようとしています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 にシリアル化していない理由はありますか? RouteValues を手動で構築するのではなく、ここでオブジェクトを使用したいと思いますnew { field = vale }

4

1 に答える 1

3

ああ、間違ったオーバーロードを使用しています。それは正常です。ASP.NET MVC チームは、この API を本当に混乱させました。呼び出しているメソッドに注意する必要があります。必要なオーバーロードは次のとおりです。

<% 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;"/>
<% } %>

正しい過負荷に注意してください。routeValuesandhtmlAttributesを匿名オブジェクトとして使用していましたが、基本的にオーバーロードを台無しにModel.FormModel.RouteValuesした として渡していました。RouteValueDictionary

F12カーソルを上に置いてヒットしBeginForm、運が良ければ、Razor ビューで Intellisense が正常に動作する場合 (これはめったに起こりません)、実際に呼び出しているメソッドにリダイレクトされ、間違いに気付くでしょう。

于 2011-11-28T23:48:04.410 に答える