asp.net-mvc-3 – MVC3 Html.BeginForm – 在RouteValueDictionary失败时传递参数失败

我有一个多步骤的设置过程,我想传递追加到URL的查询字符串参数,只要它们是相关的.

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

@using(Html.BeginForm())

工作得很好.它产生:< form action =“/ Account / Profile?setup = true& amp; cats = dogs”method =“post”>即它将任何原始查询字符串参数传递给POST操作,然后在该Controller操作中,我可以通过添加到RouteValues和RedirectToResult来选择哪些相关的传递到我的下一步,或者必须添加.

但是,我需要为表单指定一个类来进行样式设置.

我试过了:

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

产量:

< form action =“/ Account / Profile?Count = 1& amp; Keys = System.Collections.Generic.Dictionary`2 + KeyCollection [System.String,System.Object]& amp; Values = System.Collections.Generic .Dictionary`2 + ValueCollection [System.String,System.Object]“class =”mainForm“method =”post“>

(args由过滤器生成,是RouteValueDictionary).规范http://msdn.microsoft.com/en-us/library/dd505151.aspx表明您可以使用System.Web.Routing.RouteValueDictionary传递参数.

我想要的是< form action =“/ Account / Profile?setup = true& amp; cats = dogs”class =“mainForm”method =“post”>

我应该提一下,我宁愿不做一些事情,比如传递新的{key = value},因为有相当多的逻辑来确定我将传递给下一步的内容.

关于在这里做什么的任何建议?

我被这个看似简单的任务所困扰,我肯定错过了一些非常明显的东西.

解决方法

args was generated by a filter,and is a RouteValueDictionary

这是关键点.在这种情况下,请确保您使用的是BeginForm方法correct overload

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

注意最后一个参数?它必须是IDictionary< string,object>为了这个工作.

在你的例子中,它是this overload被拾取.但是,由于您为routeValues参数传递了RouteValueDictionary而不是匿名对象,因此它会搞砸.

因此,您应该将routeValues和htmlAttributes都作为字典或两者都作为匿名对象.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....