Reputation: 591
if I call the POST action method I want to get the data from the files-object of my GET action method.
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);
}
}
My View looks like this:
@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
How can I use the data from the files object of my GET Index() action method in my POST Index method?
Upvotes: 0
Views: 892
Reputation: 71
Here is the example to get the data before the post action.
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");
}
Here I will get the data from my get API method and this data is passed to the post view[HttpPost] and then post or put action can be performed.
Upvotes: 0
Reputation: 1520
There are a number of ways to do this. You could put the files in a view data dictionary in the get controller.
ViewData["Files"] = files
Then retrieve it from your post.
var files = ViewData["Files"]
You could also pass the files to a view model in your get controller, send it to your view. Then pass it to the post action when you submit the form on the view.
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){
....
}
Upvotes: 2