【发布时间】:2021-12-10 16:05:39
【问题描述】:
如果我调用 POST 操作方法,我想从我的 GET 操作方法的文件对象中获取数据。
public class UploadController:Controller {
public IActionResult Index(){
// Here is some code
return View(files);
}
[HttpPost]
public IActionResult Index(IFormFile importFile){
{
// Here I want to work with data from the files object of my Index() method above
return View("Index", newFiles);
}
}
我的视图如下所示:
@using MVC-project.Models
@model UploadViewModel
<table>
<tr>
<th>File Name</th>
<th></th>
</tr>
@foreach (string file in Model.FileName )
{
<tr>
<td>@file</td>
<td>@Html.ActionLink("Download", "DownloadFile", new { fileName = file })</td>
</tr>
}
</table>
@using (Html.BeginForm("Index", "Upload", FormMethod.Post, new { @id = "upldFrm", @enctype = "multipart/form-data" }))
{
<div class="row">
<div class="form-group col-md-6">
<input type="file" class=" form-control" name="importFile" />
</div>
<div class="form-group col-md-6">
<input type="submit" name="filesubmit" value="Upload" />
</div>
</div>
}
// Here is some code and if-case for processing after the POST submit
如何在我的 POST Index 方法中使用来自我的 GET Index() 操作方法的 files 对象的数据?
【问题讨论】:
-
HTTP 是无状态的。每个请求都会有一个新的控制器实例。虽然这两个操作都可以调用相同的私有方法。
-
这能回答你的问题吗? Pass data between Actions in MVC
-
@MiladDastanZand 是和否:D 如何在我的 Razor 代码中使用 keep 和 peek 来保留我的 ViewModel?也许在 razor 之外还有另一种方法可以保留和查看 ViewModel
标签: c# model-view-controller asp.net-mvc-5