MVC3 Html.BeginForm - 传递参数的RouteValueDictionary失败 [英] MVC3 Html.BeginForm - passing arguments as RouteValueDictionary fails

查看:330
本文介绍了MVC3 Html.BeginForm - 传递参数的RouteValueDictionary失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多步骤的安装过程中,我想通过追加到,只有当他们是相关的URL查询字符串参数。

<一个href=\"http://localhost:6618/Account/Profile?wizard=true&cats=dogs\">http://localhost:6618/Account/Profile?wizard=true&cats=dogs

@using(Html.BeginForm())

伟大的工作。它产生:&LT;形式的行动=/帐号/个人设置=真放大器;放大器;猫=狗?的方法=后&GT; 即它传递到POST行动的任何原始查询串参数,然后在该控制器的动作,我可以选择哪些是相关传递到我的下一个步骤,或需要增加,通过向RouteValues​​和RedirectToResult。

不过,我需要一个类分配给我的表格样式的目的。

我试过:

@using(Html.BeginForm(档案,帐户,指定参数时,FormMethod.Post,新{@class =的MainForm}))

这将产生:

&LT;形式action=\"/Account/Profile?Count=1&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D\"类=MainForm的方法=后&GT;

ARGS 是由过滤器生成的,并且是一个RouteValueDictionary)。该规范<一个href=\"http://msdn.microsoft.com/en-us/library/dd505151.aspx\">http://msdn.microsoft.com/en-us/library/dd505151.aspx表明你可以传递参数与System.Web.Routing.RouteValueDictionary。

我要的是&LT;形式的行动=/帐号/个人设置=真放大器;放大器;猫=狗类=MainForm的方法=后&GT;

我要提到我会preFER不会做这样的事情传递新{键=值} 来代替,因为有相当数量的逻辑来判断我将沿着下一个步骤路过。

什么任何建议在这里做?

我对这个看似简单的任务受阻,而且我肯定失去了一些东西非常明显。


解决方案

  

args在由滤波器产生的,并且是一个RouteValueDictionary


这是这里的关键点。在这种情况下,确保您使用的是正确超载:

  @using(Html.BeginForm(
    简介,
    帐户,
    ARGS,
    FormMethod.Post,
    新RouteValueDictionary(新{@class =的MainForm})
))
{
    ...
}

注意最后一个参数?它必须是一个的IDictionary&LT;字符串对象&gt; 这个工作

在您的例子是此重载是被拾起。但既然你是传递一个 RouteValueDictionary 为routeValues​​参数,而不是它被搞砸了一个匿名对象。

那么,您应该同时拥有routeValues​​和htmlAttributes如字典或两个匿名对象。

I have a multi-step setup process where I would like to pass query string arguments appended to the URL only if they are relevant.

http://localhost:6618/Account/Profile?wizard=true&cats=dogs

@using( Html.BeginForm() )

worked great. It yielded: <form action="/Account/Profile?setup=true&amp;cats=dogs" method="post"> i.e. it passed into the POST action any of the original query string parameters, and then in that Controller action I could chose which ones were relevant to pass to my next step, or necessary to add, by adding to the RouteValues and a RedirectToResult.

However, I need to assign a class to my form for styling purposes.

I tried:

@using( Html.BeginForm( "Profile", "Account", args, FormMethod.Post, new { @class = "mainForm" } ) )

which yields:

<form action="/Account/Profile?Count=1&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D" class="mainForm" method="post">

(args was generated by a filter, and is a RouteValueDictionary). The specification http://msdn.microsoft.com/en-us/library/dd505151.aspx indicates that you can pass arguments with a System.Web.Routing.RouteValueDictionary.

What I want is <form action="/Account/Profile?setup=true&amp;cats=dogs" class="mainForm" method="post">

I should mention I would prefer not to do something like passing in new {key = value} instead, since there is a fair amount of logic to determine what I will be passing along to the next step.

Any suggestions on what to do here?

I am stymied by this seemingly simple task, and am surely missing something terribly obvious.

解决方案

args was generated by a filter, and is a RouteValueDictionary

That's the key point here. In this case make sure you are using the correct overload of the BeginForm method:

@using(Html.BeginForm(
    "Profile", 
    "Account",   
    args, 
    FormMethod.Post, 
    new RouteValueDictionary(new { @class = "mainForm" })
))
{
    ...
}

Notice the last argument? It must be an IDictionary<string, object> for this to work.

In your example it is this overload that gets picked up. But since you are passing a RouteValueDictionary for the routeValues parameter instead of an anonymous object it gets messed up.

So, you should either have both routeValues and htmlAttributes as dictionaries or both as anonymous objects.

这篇关于MVC3 Html.BeginForm - 传递参数的RouteValueDictionary失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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