0

This is the action method where I save user input and then redirect to the view mode:

[HttpPost]
public ActionResult SaveDocumentCitizen(DocumentCitizen documentCitizen)
{
    DocumentCitizenRepository repository = new DocumentCitizenRepository();
    repository.SaveDocument(documentCitizen);
    return RedirectToAction(actionName: "ViewDocumentCitizen", routeValues: new RouteValueDictionary(documentCitizen));
}

And here's the ViewDocumentCitizen action method:

public ActionResult ViewDocumentCitizen(DocumentCitizen doc)// The Attachment value is null here
{
    DocumentCitizenRepository repository = new DocumentCitizenRepository();
    DocumentCitizen docCitizen = repository.UpdateTextualValues(doc.DocID);
    return View(viewName: "DocumentCitizen", model: docCitizen);
}

The DocumentCitizen model has the following property:

public byte[] Attachment{get;set;}

I choose a file then submit the form and then when I debug the SaveDocumentCitizen method I can see that the Attachment is not null. But it gets set to null as soon as it's passed to the ViewDocumentCitizen method. What do I have to do to have the file property value persisted through redirection?

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206

1 Answers1

1

I think the problem is parameter name in RouteData and in Action method are not equal . Try to modify your SaveDocumentCitizen action

[HttpPost]
public ActionResult SaveDocumentCitizen(DocumentCitizen documentCitizen)
{
    DocumentCitizenRepository repository = new DocumentCitizenRepository();
    repository.SaveDocument(documentCitizen);
    return RedirectToAction(actionName: "ViewDocumentCitizen", routeValues: new RouteValueDictionary(new RouteValueDictionary(new Dictionary<string, object> {{"doc", documentCitizen}})));
}

Or better

return RedirectToAction(actionName: "ViewDocumentCitizen", routeValues: new { doc = documentCitizen});

This should help to your ModelBinder to recognize parameter by name

EDIT:

From MSDN "Controller.RedirectToAction "

Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

Get method has a length limit, read more HERE

But .Net Framework ready for everything, and they created ControllerBase.TempData property read more on MSDN

Hope it will help you

Community
  • 1
  • 1
Baximilian
  • 885
  • 7
  • 25