0

I have a view named Index.cshtml and controller named HomeController and I can't seem to show the ViewBag.Message and ViewBag.FileUrl to the view. The code is working I just can't show the ViewBag.message when the file is successfully uploaded.

Here's my code below

Index.cshtml

@{ 
ViewBag.Title = "Upload a file to S3 Bucket";
}

<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<h3>@ViewBag.FileUrl</h3>

<p>Use this area to browse image and upload to S3 bucket.</p>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div>
    @Html.TextBox("file", "", new { type = "file" }) <br />
    <input type="submit" value="Upload" />
    @ViewBag.FileUrl
    @ViewBag.Message
</div>
}    

HomeController.cs

[HttpGet]
public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    var s3Client = new AmazonS3Client(accesskey, secretkey, bucketRegion);
    var result = "";
    var keyName = file.FileName;
    var fileTransferUtility = new TransferUtility(s3Client);

    try
    {
        if (file.ContentLength > 0)
        {
            var filePath = Path.Combine(Server.MapPath("~/App_Data"), Path.GetFileName(file.FileName));
            var fileTransferUtilityRequest = new TransferUtilityUploadRequest
            {
                BucketName = bucketName,
                FilePath = filePath,
                StorageClass = S3StorageClass.StandardInfrequentAccess,
                PartSize = 6291456, // 6 MB.  
                Key = keyName,
                CannedACL = S3CannedACL.PublicRead
            };

            fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
            fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
            fileTransferUtility.Upload(fileTransferUtilityRequest);
            fileTransferUtility.Dispose();
        }
        result = string.Format("http://{0}.s3.amazonaws.com/{1}", bucketName, keyName);

        ViewBag.FileUrl = result;
        ViewBag.Message = "File Uploaded Successfully!!";
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        ViewBag.Message = "Error occurred: " + amazonS3Exception.Message;
    }

    return RedirectToAction("Index");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JOJO
  • 81
  • 1
  • 2
  • 9

2 Answers2

3

You assign ViewBag values, which are lost by the subsequent RedirectToAction().

Found the answer here, with detailed comparison of ViewData, ViewBag, and TempData. In your case, TempData should work.

devio
  • 36,858
  • 7
  • 80
  • 143
1

You are doing redirect the result so that value becomes null, instead of that use Tempdata in where assign a value and get it where you redirect,

catch (AmazonS3Exception amazonS3Exception)
{
    Tempdata["Message"]="Error occurred: " + amazonS3Exception.Message;
}

and Index action method, try it like below ,

[HttpGet]
public ActionResult Index()
{
    ViewBag.Message= (string)Tempdata["Message"]
    return View();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ranjith.V
  • 316
  • 1
  • 7
  • You need to spell correctly: `TempData` (with a **capital D** ) - not "Tempdata" - correct spelling is **important** and relevant in C# / ASP.NET MVC ! – marc_s Jun 01 '20 at 07:53
  • This worked! Thanks! :) I'm new to asp.net mvc so I get confused sometimes :) – JOJO Jun 01 '20 at 08:06