c# - 如何将参数从 excel post 传递到 Controller 操作

标签 c# asp.net-mvc excel http-post vba

如何将发布的参数传递给 Controller ​​操作?无法在 URL 中传递它,因为参数很大:这是我用来发布参数的 excel 代码:

Sub PostDataTest()
Dim PostData As String
Dim Comments As String
Dim PostDataURL As Srting

PostDataURL = "http://localhost:11121/InsertData/TestData/"

Comments = Me.Comments.Value

    Set httpReq = New MSXML2.xmlhttp

    httpReq.Open "POST", PostDataURL, False

    httpReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

    PostData = "Comments=" & Comments

    httpReq.send PostData
     PostData = ""
    Set httpReq = Nothing

End Sub

这是我的 Controller 操作:我无法在 URL 中传递“评论”,因为它很长 有没有其他方法可以将此变量传递给下面的 Controller 操作?

[HttpPost]
public ActionResult TestData(string Comments)
{
    TestData.Comments = Comments;
    DataContext.InsertTestData(TestData);      
}

最佳答案

您的 POST 数据应采用“key = value”格式。

你在哪里

PostData = Comments

应该是

PostData = "Comments=" & Comments

否则, Controller 操作请求中的 POST 数据可能为空,或者 MVC 可能无法自动将该值绑定(bind)到操作方法中的 Comments 参数。

引用以下帖子:

如果绑定(bind)到 Comments 不起作用,您可以尝试通过将操作定义更改为以下方式从请求中提取值:

[HttpPost]
public ActionResult TestData(FormCollection form)
{
    TestData.Comments = form["Comments"];
    DataContext.InsertTestData(TestData);      
}

关于c# - 如何将参数从 excel post 传递到 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8344650/

相关文章:

c# - Tableadapter 不更新

c# - 如何在变量值更改时触发事件?

c# - 登录后重定向 : Web. 配置

c# - 在 ASP.NET MVC Core 应用程序 (RC2) 中显示项目版本

ASP.NET MVC : Returning large amounts of data from FileResult

function - 如何编写打开和关闭数据库连接的常用函数?

c# - Web API 重定向到网页

python - 使用 python 对 Excel 文件进行排序并发送每日电子邮件

vba - Excel VBA IF ElseIf

javascript - 单选按钮的选定值不会改变