asp.net-mvc 提交表单并将数据传递给 FileStreamResult 类型的控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14124324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submitting form and pass data to controller method of type FileStreamResult
提问by jpo
I have an mvc form (made from a model) which when submitted, I want to get a parameter I have the code to set the form and get the parameter
我有一个 mvc 表单(由模型制成),在提交时,我想获取一个参数我有设置表单并获取参数的代码
using (@Html.BeginForm("myMethod", "Home", FormMethod.Get, new { id = @item.JobId })){
}
and inside my home controller I have
在我的家庭控制器里面我有
[HttpPost]
public FileStreamResult myMethod(string id)
{
sting str = id;
}
However, I always get the error
但是,我总是收到错误
The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
您正在寻找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。请检查以下 URL 并确保其拼写正确。
When I omit the [HttpPost], the code executes file but the variables strand idare null.
How can I fix this please?
当我省略了[HttpPost],该代码执行文件,但变量str和id为空。请问我该如何解决?
EDIT
编辑
Can this be caused because myMethod in the controller is not an ActionResult? I realized that when I have a method of type Actionresult where the method is bound to a view, everything works well. But the type FileStreamresult cannot be bound to a View. How can I pass data to such methods?
这可能是因为控制器中的 myMethod 不是 ActionResult 造成的吗?我意识到当我有一个 Actionresult 类型的方法时,该方法绑定到一个视图,一切正常。但是 FileStreamresult 类型不能绑定到视图。如何将数据传递给这些方法?
回答by Forty-Two
When in doubt, follow MVC conventions.
如有疑问,请遵循 MVC 约定。
Create a viewModel if you haven't already that contains a property for JobID
如果您还没有包含 JobID 的属性,请创建一个 viewModel
public class Model
{
public string JobId {get; set;}
public IEnumerable<MyCurrentModel> myCurrentModel { get; set; }
//...any other properties you may need
}
Strongly type your view
强烈输入您的观点
@model Fully.Qualified.Path.To.Model
Add a hidden field for JobId to the form
将 JobId 的隐藏字段添加到表单中
using (@Html.BeginForm("myMethod", "Home", FormMethod.Post))
{
//...
@Html.HiddenFor(m => m.JobId)
}
And accept the model as the parameter in your controller action:
并接受模型作为控制器操作中的参数:
[HttpPost]
public FileStreamResult myMethod(Model model)
{
sting str = model.JobId;
}
回答by CoffeeCode
This is because you have specified the form method as GET
这是因为您已将表单方法指定为GET
Change code in the view to this:
将视图中的代码更改为:
using (@Html.BeginForm("myMethod", "Home", FormMethod.Post, new { id = @item.JobId })){
}
回答by Alex
You seem to be specifying the form to use a HTTP 'GET' request using FormMethod.Get. This will not work unless you tell it to do a post as that is what you seem to want the ActionResult to do. This will probably work by changing FormMethod.Getto FormMethod.Post.
您似乎正在指定使用 HTTP 'GET' 请求的表单FormMethod.Get。除非您告诉它做一个帖子,否则这将不起作用,因为这似乎是您希望 ActionResult 做的。这可能会通过更改FormMethod.Get为FormMethod.Post.
As well as this you may also want to think about how Get and Post requests work and how these interact with the Model.
除此之外,您可能还想考虑 Get 和 Post 请求如何工作以及它们如何与模型交互。
回答by Anto Subash
here the problem is model binding if you specify a class then the model binding can understand it during the post if it an integer or string then you have to specify the [FromBody] to bind it properly.
这里的问题是模型绑定,如果您指定一个类,那么模型绑定可以在发布期间理解它,如果它是整数或字符串,那么您必须指定 [FromBody] 以正确绑定它。
make the following changes in FormMethod
在 FormMethod 中进行以下更改
using (@Html.BeginForm("myMethod", "Home", FormMethod.Post, new { id = @item.JobId })){
}
}
and inside your home controller for binding the string you should specify [FromBody]
在你的家庭控制器中绑定你应该指定的字符串 [FromBody]
using System.Web.Http;
[HttpPost]
public FileStreamResult myMethod([FromBody]string id)
{
// Set a local variable with the incoming data
string str = id;
}
FromBody is available in System.Web.Http. make sure you have the reference to that class and added it in the cs file.
FromBody 在 System.Web.Http 中可用。确保您拥有对该类的引用并将其添加到 cs 文件中。

