Christian Sauer
Christian Sauer

Reputation: 10899

Complex Type with ValidationMessage

I have a project where some internalization is needed. Specifically, I have a class called "LocalizedString" which holds the english and german translation of a particular text.

this looks like this:

 [ComplexType]
 public class LocalizedString : IComparer, IComparable
 {
   public string EnglishText { get; set; }
   public string GermanText { get; set; }
// this is only an example - the real class has some methods to return the text in the current language.
     }

The class is used in nearly all my domain and view models like this:

public class DemoItem
{
  public LocalizedString ItemDescription {get; set;}
}

Finally, the DemoItem might be rendered like this:

@model Domain.Entities.DemoItem

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>DemoItem</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ItemDescription , htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ItemDescription , new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ItemDescription , "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Now the problem is, that the EditorFor method renders two text-boxes as input fields for the ItemDescription - which is perfectly fine and should look like this. BUT if there is an error, e.g. the user forgot to enter a german description, ValidationMessageFor() does not work. Or more specifically: No error is displayed to the user, because the items supplied by the postback are not in the expected format. Displaying all errors via ValidationSummary works, but is not as nice as errors right next to the offending element.

Is there an easy way to get the ValidationMessages specific to the offending element?

Upvotes: 2

Views: 236

Answers (1)

Rajnikant
Rajnikant

Reputation: 2236

if you use DataAnnotation attributes for your properties in your LocalizedString class then validation messages will appear next to the offending elements.

I added validation attributes to the GermanText And EnglishText as below

    [Required]
    public string EnglishText { get; set; }

    [Required]
    public string GermanText { get; set; }

And able to see the validation messages next to the offending elements. Doing this I am able to see validation messages next to each of offending elements.

I hope this will hep.

Upvotes: 1

Related Questions