在服务器上创建RouteValueDictionary和ASPX使用? [英] Create RouteValueDictionary on server and use in aspx?

查看:127
本文介绍了在服务器上创建RouteValueDictionary和ASPX使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林传递 RouteValueDictionary 来我的aspx,这样我可以把它作为参数的 Ajax.BeginForm 方法。我打开它,像这样:

  RouteValues​​ =新System.Web.Routing.RouteValueDictionary();

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

然后将其添加到我的模式没有问题。当我把它作为参数传递给 BeginForm 方法,它呈现的动作,如下:

<$p$p><$c$c>/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 code:

 (Ajax.BeginForm(Model.FormModel.Action,
    Model.FormModel.Controller,
    Model.FormModel.RouteValues​​,
新AjaxOptions {
    HttpMethod =邮报,
    InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
    UpdateTargetId =WindowContent
},{新的id = FormId})){%GT;
&LT;输入名称=提交类型=提交级=按钮值=的风格=浮动:权利;/&GT;
&LT;%} //前端形状%GT;
 

下面是重新presents视图模型Model.FormModel

 公共类FormViewModel {

    公共字符串操作{获得;组; }

    公共字符串控制器{获得;组; }

    公共字符串的方法{获取;组; }

    公共RouteValueDictionary RouteValues​​ {获得;组; }
}
 

任何想法,为什么它不序列化RouteValueDictionary到行动上正确的URL?我想用一个对象在这里,而不是打造RouteValues​​用手新{字段=谷}

解决方案

啊,你使用了错误的过载。这是正常的。在ASP.NET MVC球队真正改坏了这个API。你必须要小心,要调用哪个方法。这里是过载,你需要:

 &LT;%使用(Ajax.BeginForm(
    Model.FormModel.Action,// actionName
    Model.FormModel.Controller,// controllerName
    Model.FormModel.RouteValues​​,// routeValues
    新AjaxOptions {// ajaxOptions
        HttpMethod =邮报,
        InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
        UpdateTargetId =WindowContent
    },
    新的字典&LT;字符串,对象&gt; {{ID,FormId}})// htmlAttributes
){%&GT;
    &LT;输入名称=提交类型=提交级=按钮值=的风格=浮动:权利;/&GT;
&LT;%}%GT;
 

注意正确的过载?您正在使用一个正在采取的 routeValues​​ htmlAttributes 为匿名对象,除非你是路过 Model.FormModel.RouteValues​​ RouteValueDictionary 基本上crapped你的过载。

按<大骨节病> F12 盘旋而将光标放在 BeginForm ,如果你足够幸运和智能的剃刀意见工作正常,你(这很少发生),你会被重定向到你实际上是调用,实现自己的错误的方法。

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 }

解决方案

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.

这篇关于在服务器上创建RouteValueDictionary和ASPX使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆