kez
kez

Reputation: 2313

show exception error message in view

I'm trying to get exception message like this and pass to FileUpload view like this in controller class

        catch (Exception ex)
        {
            ViewBag.Error = ex.Message;                
        }

        return RedirectToAction("FileUpload", "FileUpload",ViewBag.Error);
    }

In FileUpload view page I'm trying to show that error messge like this

@ViewBag.Error

But I cannot see any error message here

EDIT

    [HttpGet]
    public ActionResult FileUpload()
    {
       ..
       return View(p);
    }

    [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase file)
    {
        ..
        return RedirectToAction("FileUpload", "FileUpload");
    }

Upvotes: 0

Views: 3311

Answers (2)

Amit Kumar
Amit Kumar

Reputation: 5962

you need to accept that message in FileUpload action then again assign it to viewbag to get data in page.

   string exmsg="";
    try
    {
    //....
    }
    catch (Exception ex)
    {
     exmsg = ex.Message;                
    }
    return RedirectToAction("FileUpload", "Controllername", new { errormsg = exmsg });
    }

[HttpGet]
    public ActionResult FileUpload(string errormsg)
    {
       ViewBag.Error=errormsg
       return View(errormsg);
    }

Upvotes: 2

SᴇM
SᴇM

Reputation: 7213

try this:

catch (Exception ex)
            {
                return RedirectToAction("FileUpload", "FileUpload", new { errorMessage = ex.Message});                
            }

and the Get method

[HttpGet]
public ActionResult FileUpload(string errorMessage)
{
   @ViewBag.Error = errorMessage;
   . . .
   return View(p);
}

and I believe that the second argument of RedirectToAction("FileUpload","FileUpload") is your Controller name.

Upvotes: 1

Related Questions