加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 提交表单并将数据传递给FileStreamResult类型的

发布时间:2020-12-15 18:39:01 所属栏目:asp.Net 来源:网络整理
导读:我有一个mvc表单(由一个模型),当提交时,我想获取一个参数 我有代码来设置表单并获取参数 using (@Html.BeginForm("myMethod","Home",FormMethod.Get,new { id = @item.JobId })){} 并在我的家庭控制器里面 [HttpPost] public FileStreamResult myMethod(st
我有一个mvc表单(由一个模型),当提交时,我想获取一个参数
我有代码来设置表单并获取参数
using (@Html.BeginForm("myMethod","Home",FormMethod.Get,new { id = @item.JobId })){
}

并在我的家庭控制器里面

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

    }

但是,我总是得到错误

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]时,代码执行文件,但是变量str和id为null。
请问如何解决?

编辑

这可能是因为控制器中的MyMethod不是ActionResult引起的?我意识到,当我有一种类型为Actionresult的方法,其方法绑定到一个视图,一切都很好。但是FileStreamresult类型不能绑定到View。如何将数据传递给这些方法?

解决方法

如有疑问,请遵循MVC惯例。

如果您还没有包含JobID的属性,请创建一个viewModel

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添加一个隐藏的字段

using (@Html.BeginForm("myMethod",FormMethod.Post))
{   
    //...    
    @Html.HiddenFor(m => m.JobId)
}

并接受模型作为您的控制器操作中的参数:

[HttpPost]
public FileStreamResult myMethod(Model model)
{
    sting str = model.JobId;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读