Is there a way to only display ValidationSummary errors associated with a partial view?

32 views Asked by At

I building a form in .NET5. I have view which has several nested partial views which reference different levels of my model. I perform validation and display any errors below the field. I also display a summary of the errors using ValidationSummary.

What I would like to do is use the ValidationSummary at the partial view level. Meaning, if there are errors in the first partial view, the validation summary will display only the errors associated with that partial view. So far, I've not been able to accomplish this and I'm wondering if this is even possible.

For some context, this is a large dynamic form with several collapsible fields. The parent view contains several nested partial views, and each partial view may also have nested partial views. The specific partial views rendered are determined at runtime based on the form type. Each partial view is passed only the "model level" needed in the RenderPartialAsync() call.

Example of my Model

public class Customer 
{
    public Address Address { get; set; }
    public History History { get; set; }
}

public class Address 
{
    [Required]
    public string StreetAddress { get; set; }
    
    [Required]
    public string City { get; set; }

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

public class History 
{
    [Required]
    public string SomeOtherProperty { get; set; }
}

Example of my View with nested partials:

@model Customer

<div class="row">
    <div class="table-responsive table-responsive pl-1">
   @{
        await Html.RenderPartialAsync("_Address",
        Model.Address,
        new ViewDataDictionary(ViewData){});
    }
    </div>
    <div class="table-responsive table-responsive pl-1">
   @{
        await Html.RenderPartialAsync("_History",
        Model.History,
        new ViewDataDictionary(ViewData){});
    }
    </div>
</div>

Example of my partial view _address

@model Address

<div>
    <div>
        <table>
            <thead>
                <th>Address</th>
                <th>City</th>
                <th>State</th>
                <th>Zip</th>
            </thead>
            <tbody>
                <tr>
                    <input asp-for="Model.StreetAddress" />
                    <span class="text-danger" asp-validation-for="Model.StreetAddress"></span>
                </tr>
                <tr>
                    <input asp-for="Model.City" />
                    <span class="text-danger" asp-validation-for="Model.StreetAddress"></span>
                </tr>
                <tr>
                    <input asp-for="Model.ZipCode" />
                    <span class="text-danger" asp-validation-for="Model.StreetAddress"></span>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="text-danger" asp-validation-summary>
        @* Here is where I want to display a summary of errors for Address *@
    </div>
</div>

Example of my partial view _history

@model History

<div>
    <div>
        <table>
            <thead>
                <th>Label</th>
                <th>...</th>                
            </thead>
            <tbody>
                <tr>
                    <input asp-for="Model.SomeOtherProperty" />
                    <span class="text-danger" asp-validation-for="Model.StreetAddress"></span>
                </tr>
                <tr>
                    <input asp-for="Model..." />
                    <span class="text-danger" asp-validation-for="Model.StreetAddress"></span>
                </tr>
                
            </tbody>
        </table>
    </div>
    <div class="text-danger" asp-validation-summary>
        @* Here is where I want to display a summary of errors for History *@
    </div>
</div>

Unfortunately, this approach has failed and instead, both validation summary calls in my partial views display all errors on the page, thus Address errors are displayed in the summary of the partial _history view.

Note, this is just an example of what I'm trying to achieve.

0

There are 0 answers