Create RouteValueDictionary on server and use in aspx?

别说谁变了你拦得住时间么 提交于 2019-12-11 06:59:18

问题


I am tring to pass a RouteValueDictionary to my aspx so that I can use it as the parameters for an Ajax.BeginForm method. I load it up like so:

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

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

and then add it to my model without issue. When I put it as the parameter to the BeginForm method it renders the action as this:

/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

Here is the aspx code:

(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 %>

Here is the View Model that represents 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; }
}

Any idea why it is not serializing the RouteValueDictionary into the proper URL on the action? I would like to use an object here rather than build the RouteValues by hand with new { field = vale }


回答1:


Ah, you are using the wrong overload. It's normal. The ASP.NET MVC team really made a mess out of this API. You gotta be careful which method you are invoking. Here's the overload that you need:

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

Notice the correct overload? You were using the one that was taking routeValues and htmlAttributes as anonymous objects, except that you was passing Model.FormModel.RouteValues as a RouteValueDictionary which basically crapped your overload.

Hit F12 while hovering the cursor over the BeginForm and if you are lucky enough and Intellisense works fine for you in Razor views (which rarely happens) you will get redirected to the method you are actually invoking and realize your mistake.



来源:https://stackoverflow.com/questions/8303973/create-routevaluedictionary-on-server-and-use-in-aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!