简体   繁体   中英

How get value from current controller?

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?

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){
.... 
} 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM