3

I am curious as to how to do this, I want to send a custom error message for forums and for regular views from a controller to a view (thus looking for examples of both). I have seen ModelState.AddError() how ever I am looking to control the look the error message that is be able to wrap it in divs on the front end.

any ideas?

I am very new to ASP.net and the tutorials out there for this seem very confusing to me. So I don't really have any code to show as I am not sure what I am suppose to do.

0

2 Answers 2

6

I believe you are looking to customize the error message on your view. You can customize the error messages including the ones you added using ModelState.AddModelError("", "error message") in three ways.

Option 1

Use Html.ValidationSummary() and customize its look via the validation-summary-errors class. The default mvc project template in VS will include that class by default. It has a default color of red (I think). But you can practically control every aspect of the error summary by changing that class.

.validation-summary-errors {
    border: 5px solid red; // thick red border
    color: #3904D9; // dark blue
    font-size: 1.5em; // quite big fonts
    font-weight: bold;
}

Option 2

The ValidationSummary outputs the messages in an unordered list (li). If your project calls for a different way of showing the error messages, you can do it on the view by inspecting the ViewDataDictionary. The following code will write the error messages in separate divs with a thin red border. The style is written inline to make the sample simple.

@foreach (var item in ViewData.ModelState) {
    if (item.Value.Errors.Any()) {
        foreach (ModelError e in item.Value.Errors) {
            <div style="border: 1px solid red;margin-bottom:5px;">
                @e.ErrorMessage</div>
        }
    }
}

Option 3

If you do not want to put logic on your view, then you can extend the htmlhelper (or create a custom helper). You basically will use the same logic shown in Option 2 in the custom htmlhelper.

public static class HtmlExtensions {
    public static MvcHtmlString CustomValidationSummary(this HtmlHelper helper)
    {
        var html = string.Empty;
        foreach (var item in helper.ViewData.ModelState)
        {
            if (item.Value.Errors.Any()) {
                foreach (ModelError e in item.Value.Errors) {
                    html 
                      += "<div style='border: 1px solid red;margin-bottom:5px;'>" 
                      + e.ErrorMessage 
                      + "</div>";
                }
            }
        }

        return MvcHtmlString.Create(html);
    }
}

And then you use it the same way as you will use the ValidationSummary:

@Html.CustomValidationSummary()
Sign up to request clarification or add additional context in comments.

3 Comments

I am wondering what you do to send a error from the controller to the view? is it ModelState.AddError()?
@TheWebs What exactly are you trying to do ? Form Validations ?
@TheWebs by using ModelState.AddModelError
2

You may want to look into the ViewBag object. If you set ViewBag.ErrorMessage in your controller, it will be available as a string in your view. And, actually, you can name it whatever you want, not just ErrorMessage.

Add this and include logic in your view to only display if !string.IsNullOrEmpty(ViewBag.ErrorMessage) and you should be good.

This is only one of multiple ways to do it. ViewBag is just a wrapper on ViewData, so if you are stuck, you may want to look into that object also.

While this answer is not fully comprehensive, hopefully it will give you a place to start.

Best of luck!!

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.