Question

I would like to show error message with alert box in asp.net MVC3 Razor. I used dataanootation for my model. Please see below.

<Required(ErrorMessage:="Name is required")> _
Public Name as string

In client side.

@Html.TextBoxFor(Function(model) model.Content)
@Html.ValidationMessageFor(Function(model) model.Content, "Please type name")
@Html.ValidationSummary()

But the error message show as a label beside of textbox. I want to show only alert box for error message. Thanks all.

Was it helpful?

Solution

<script type="text/javascript">
    @if (!ViewContext.ViewData.ModelState.IsValid)
    {
        var sb = new StringBuilder();
        foreach (var modelState in ViewContext.ViewData.ModelState.Values)
        {
            foreach (var error in modelState.Errors)
            {
                sb.Append(error.ErrorMessage);
            }
        }
        @:alert('@sb.ToString()');
    }
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top