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

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

问题描述

我有一个 mvc 表单(由模型制成),在提交时,我想获取一个参数我有设置表单和获取参数的代码

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 })){
}

在我的家庭控制器里面

    [HttpPost]
    public FileStreamResult myMethod(string id)
    {
         sting str = id;

    }

但是,我总是收到错误

您正在寻找的资源(或其依赖项之一)可能已被删除,更名,或暂时不可用.请检查以下 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] 时,代码执行文件但变量 strid 为空.请问我该如何解决?

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.

如果您还没有包含 JobID 的属性,请创建一个视图模型

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天全站免登陆