【问题标题】:How get value from current controller?如何从电流控制器中获取价值?
【发布时间】: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


【解决方案1】:

有很多方法可以做到这一点。您可以将文件放在 get 控制器中的视图数据字典中。

ViewData["Files"] = files

然后从您的帖子中检索它。

var files = ViewData["Files"] 

您还可以将文件传递到 get 控制器中的视图模型,将其发送到您的视图。然后在您在视图上提交表单时将其传递给 post 操作。

public class ViewModel {
     public string Files {get; set;}
     public IFormFile File {get; set;} 
} 


[HttpGet]
public IActionResult Index(){
    var viewModel = new ViewModel 
    {
        Files = files
    };
    return View(viewModel);
} 

[HttpPost] 
public IActionResult Index(ViewModel viewModel){
.... 
} 

【讨论】:

  • 感谢您的回答。我尝试了您的解决方案,但 POST 操作中的 ViewModel 参数为空... :(
  • @Christian01 你是如何在你的视图中绑定它的?请分享观点
【解决方案2】:

这是在发布操作之前获取数据的示例。

public ActionResult Edit(int id)
{
  HttpResponseMessage response =GlobalVariables.webApiClient.GetAsync("Tbl_Books/"+ id.ToString()).Result;

  return View(response.Content.ReadAsAsync<Books>().Result);
}

[HttpPost]
public ActionResult Edit(Books newbook)
{

  HttpResponseMessage response =GlobalVariables.webApiClient.PostAsJsonAsync("Tbl_Books", newbook).Result;
            
  HttpResponseMessage response = 
  GlobalVariables.webApiClient.PutAsJsonAsync("Tbl_Books/" + newbook.BookId, newbook).Result;

  return RedirectToAction("Index");
}

在这里,我将从我的 get API 方法中获取数据,并将此数据传递给 post view[HttpPost],然后可以执行 post 或 put 操作。

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多