gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:57:57 25 4
gpt4 key购买 nike

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

using (@Html.BeginForm("myMethod", "Home", FormMethod.Get, new { id = @item.JobId })){
}

在我的家庭 Controller 内我有

    [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]时,代码执行文件,但变量strid为空。请问我该如何解决这个问题?

编辑

这是否是因为 Controller 中的 myMethod 不是 ActionResult 导致的?我意识到,当我有一个 Actionresult 类型的方法(该方法绑定(bind)到 View )时,一切都会正常工作。但 FileStreamresult 类型不能绑定(bind)到 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", "Home", FormMethod.Post))
{
//...
@Html.HiddenFor(m => m.JobId)
}

并接受模型作为 Controller 操作中的参数:

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

关于asp.net-mvc - 提交表单并将数据传递给 FileStreamResult 类型的 Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com