0

Is there any standard practice to display errors in a view? Currently it is being displayed from TempData.

I implemented a derived a class from Base Controller and used that derived class in every one of my controller. Then assign error or success messages from controller.

public class TestController : Controller
{
    public string ErrorMessage
    {
        get { return (string) TempData[CommonHelper.ErrorMessageKey]; }

        set
        {
            if (TempData.ContainsKey(CommonHelper.ErrorMessageKey))
            {
                TempData[CommonHelper.ErrorMessageKey] = value;
            }
            else
            {
                TempData.Add(CommonHelper.ErrorMessageKey,value);
            }

            TempData.Remove(CommonHelper.SuccessMessageKey);
        }
    }

    public string SuccessMessage
    {
        get { return (string)TempData[CommonHelper.SuccessMessageKey]; }

        set
        {
            if(TempData.ContainsKey(CommonHelper.SuccessMessageKey))
            {
                TempData[CommonHelper.SuccessMessageKey] = value;
            }
            else
            {
                TempData.Add(CommonHelper.SuccessMessageKey, value);
            }

            TempData.Remove(CommonHelper.ErrorMessageKey);
        }
    }
}

CommonHelper Class

public class CommonHelper
{

    public const string SuccessMessageKey = "successMessage";

    public const string ErrorMessageKey = "errorMessage";


    public static string GetSuccessMessage(object data)
    {
        return data == null ? string.Empty : (string) data;
    }


    public static string GetErrorMessage(object data)
    {
        return data == null ? string.Empty : (string) data;
    }

}

Then created a partial view having this

@using Web.Helpers

@if (!string.IsNullOrEmpty(CommonHelper.GetSuccessMessage(TempData[CommonHelper.SuccessMessageKey])))
{
<div class="alert alert-success">
    @CommonHelper.GetSuccessMessage(TempData[CommonHelper.SuccessMessageKey])
</div>
}
else if   (!string.IsNullOrEmpty(CommonHelper.GetErrorMessage(TempData[CommonHelper.ErrorMessageKey])))
{
<div class="alert alert-success">
    @CommonHelper.GetErrorMessage(TempData[CommonHelper.ErrorMessageKey])
</div>
}

And in every view, the partial view is rendered.

<div>
        @Html.Partial("_Message")
    </div>

2 Answers 2

1

Here is a pretty common implementation of displaying errors.

Controller

public class UserController : Controller 
{
     [HttpPost]
     public ActionResult Create(User model)
     {
         //  Example of manual validation            
         if(model.Username == "Admin")
         {
            ModelState.AddModelError("AdminError", "Sorry, username can't be admin")
         }

         if(!ModelState.IsValid() 
         {
            return View(model)
         }
     }
}

Model

public class User
{
    [Required]
    public string Username {get; set;}

    public string Name {get; set; }
}

View

@Html.ValidationSummary(true)
@using(Html.BeginForm())
{
   // Form Html here 
}

You don't need all of the infrastructure you created. This is handled by the framework. If you need a way to add success messages you can checkout the Nuget Package MVC FLASH

Sign up to request clarification or add additional context in comments.

Comments

1

I prefer to use ModelState.AddModelError()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.