JOJO
JOJO

Reputation: 91

ViewBag.Message not showing ASP.NET MVC c#

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");
}

Upvotes: 0

Views: 2531

Answers (2)

Ranjith.V
Ranjith.V

Reputation: 326

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();
}

Upvotes: 2

devio
devio

Reputation: 37215

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.

Upvotes: 3

Related Questions