Mohammed Faruk
Mohammed Faruk

Reputation: 495

Make custom error message display in MVC3 Razor view engine

I have a view that design view like :

My View design mode

Here i impose some entry validation when click save button I want to display me error message in my expecting display region. How can it possible?

My Controller Action is :

 [HttpPost]
        public ActionResult Save(COA_ChartsOfAccount oCOA_ChartsOfAccount)
        {
            try
            {
                if (this.ValidateInput(oCOA_ChartsOfAccount))
                {
                    COA_ChartsOfAccount oParent = new COA_ChartsOfAccount();
                    oParent = oParent.Get(oCOA_ChartsOfAccount.ParentHeadID);
                    if (oCOA_ChartsOfAccount.IsChild)
                    {
                        oCOA_ChartsOfAccount.ParentHeadID = oParent.AccountHeadID;
                    }
                    else 
                    {
                        oCOA_ChartsOfAccount.ParentHeadID = oParent.ParentHeadID;
                    }
                    oCOA_ChartsOfAccount = oCOA_ChartsOfAccount.Save();
                    return RedirectToAction("RefreshList");
                }
                return View(oCOA_ChartsOfAccount);
            }
            catch (Exception ex)
            {                
                return View(oCOA_ChartsOfAccount);              
            }

        }

Note : I want to make common partial view for error message display. (Like exception error message, validation message, all kind of user notification message)

Upvotes: 0

Views: 5763

Answers (1)

user1166147
user1166147

Reputation: 1610

With your current set up

To display an error message

In your controller:

catch (Exception ex)
        {                
            TempData["message"] = "Custom Error Messge";
            return View(oCOA_ChartsOfAccount);              
        }

In your view:

<div style="color: red;font-weight:900;">@TempData["message"]</div>

Upvotes: 2

Related Questions