0

I have a form with one image-upload and a Submit button..

When i press the submit button, I can read the uploaded-image info in my controller (see image 1 )

The problem is, when i from this controller pass the uploaded-image info to another controller, see image 2

The "another" controller dont get the image info, its HttpPostedFileBase is null / 0

Why does it do that, what can I do ?

[HttpPost]
[UserAuthorize(Roles = "User")]
[ValidateAntiForgeryToken]
public ActionResult NewProject(NewUserProjectViewModel model)
{
        return RedirectToAction("previewProject", model);
}


[UserAuthorize(Roles = "User")]
public ActionResult previewProject(NewUserProjectViewModel model)
{
    return View(model);
}   

IMAGE 1 IMAGE 2

Imran Khan
  • 39
  • 1
  • 4

1 Answers1

0

RedirectToAction actually sends a 302 response to the browser with the new url as the location header value and the browser will read this response and issue a totally new GET request to the new url. You cannot pass a complex model with RedirectToAction.

You have few options

  1. You can directly call the PreviewProject view from your NewProject action method and pass the object.

    public ActionResult NewProject(NewUserProjectViewModel model)
    {
       return View("previewProject", model);
    }
    
  2. You can persist the model data and pass a unique id to the next action method when you send a RedirectResult response. In the second action method, use a parameter for this unique id and using which you can get the data again and use that.

    public ActionResult NewProject(NewUserProjectViewModel model)
    {
       var id = SaveModelAndReturnUniqueID(model);
       return RedirectToAction("previewProject", new {id=id});
    }
    public ActionResult previewProject(int id)
    {
       NewUserProjectViewModel model= GetNewUserProjectViewModel(id);
       return View(model);
    }
    
  3. Use a temporary persisting mechanism like Session or TempData.

Take a look at How do I include a model with a RedirectToAction?

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497