提交表单和数据传递到控制器类型FileStreamResult的方法 [英] Submitting form and pass data to controller method of type FileStreamResult

查看:343
本文介绍了提交表单和数据传递到控制器类型FileStreamResult的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC的形式(从模型所做的),它提交的时候,我希望得到一个参数
我有code设置的形式和获取参数

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

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

您正在寻找(或它的一个依赖),可以在资源
  已被删除,更名或暂时
  不可用。请检查以下URL并确保它是
  拼写正确。

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.

当我省略了 [HttpPost] 中,code执行文件,但这些变量 STR ID 为空。
我怎么能解决这个问题吗?

When I omit the [HttpPost], the code executes file but the variables str and id are null. How can I fix this please?

修改

这能造成的,因为在myMethod的控制器是不是一个ActionResult?我意识到,当我有那里的方法被绑定到一个视图类型的ActionResult的方法,一切正常。但类型FileStreamresult不能绑定到一个视图。我怎样才能将数据传递给这样的方法?

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?

推荐答案

当有疑问,请按照MVC约定。

When in doubt, follow MVC conventions.

创建一个视图模型,如果你还没有包含作业ID属性

Create a viewModel if you haven't already that contains a property for JobID

public class Model
{
     public string JobId {get; set;}
     public IEnumerable<MyCurrentModel> myCurrentModel { get; set; }
     //...any other properties you may need
}

强类型视图

@model Fully.Qualified.Path.To.Model

有关的JobId添加一个隐藏字段的形式

Add a hidden field for JobId to the form

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;
}

这篇关于提交表单和数据传递到控制器类型FileStreamResult的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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