如何将发布的参数传递给 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/