如何动态设置 ValidationMessage.For 属性? 自 For类型为 Expression-6ren">
gpt4 book ai didi

c# - 如何在 Blazor 中动态设置 ValidationMessage.For 属性?

转载 作者:行者123 更新时间:2023-12-03 21:44:36 24 4
gpt4 key购买 nike

本款Docs描述了如何显示验证消息。

<ValidationMessage For="() => Parameters.PropertyA"></ValidationMessage>      
如何动态设置 ValidationMessage.For 属性?
For类型为 Expression<Func<TValue>> ,我想传递一个 Func ,但这不会编译:
[Parameter]
public Func<string> PropertyLocator { get; set; }

<ValidationMessage For="PropertyLocator"></ValidationMessage>
这会编译,但不会正确解析验证消息
<ValidationMessage For="() => PropertyLocator"></ValidationMessage>
我还尝试使组件通用,以便它知道 Parameters类型:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Components;

public partial class MyComponent<TParam>
{
[Parameter]
public TParam Parameters { get; set; }

[Parameter]
public Func<TReportParam, string> PropertyLocator { get; set; }
}


@using System.Linq.Expressions
@typeparam TParam
<ValidationMessage For="@((Expression<Func<string>>)(() => PropertyLocator(this.Parameters)))"></ValidationMessage>


<MyComponent TParam="MyParameters" Parameters="BindToSomeValue" PropertyLocator="(parameters) => parameters.PropertyA" />
但这会导致以下运行时异常:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]Unhandled exception rendering component: The provided expression contains a InvocationExpression1 which is not supported.FieldIdentifier only supports simple member accessors (fields,properties) of an object. System.ArgumentException: The providedexpression contains a InvocationExpression1 which is not supported.FieldIdentifier only supports simple member accessors (fields,properties) of an object. atMicrosoft.AspNetCore.Components.Forms.FieldIdentifier.ParseAccessor[String](Expression`1accessor, Object& model, String& fieldName) atMicrosoft.AspNetCore.Components.Forms.FieldIdentifier.Create[String](Expression`1accessor) atMicrosoft.AspNetCore.Components.Forms.ValidationMessage`1[[System.String,System.Private.CoreLib, Version=5.0.0.0, Culture=neutral,PublicKeyToken=7cec85d7bea7798e]].OnParametersSet() atMicrosoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()atMicrosoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

最佳答案

我创建了一个小示例页面。
模型使用DataAnnotations作为验证机制。


public class DemoInputModel
{
[Required]
public String PropertyOne { get; set; }

[MinLength(2)]
public String PropertyTwo { get; set; }

[MaxLength(5)]
public String PropertyThree { get; set; }
}

在页面上,模型被初始化并设置为编辑上下文。我们有三个文本输入和一个选择框。选择框可用于切换验证消息。如果选择框的值被改变,一个新的表达式被分配给 ValidationMessage .

@using System.ComponentModel.DataAnnotations;
@using System.Linq.Expressions;

@page "/test"


<h1>ValidationMessageTest</h1>

<EditForm Model="_model">
<DataAnnotationsValidator />

<ValidationMessage For="ValidationResolver"></ValidationMessage>

<InputText @bind-Value="_model.PropertyOne" />
<InputText @bind-Value="_model.PropertyTwo" />
<InputText @bind-Value="_model.PropertyThree" />

<InputSelect @bind-Value="SelectedValidationProperty">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</InputSelect>

@*<ValidationSummary />*@

</EditForm>


@code {

private DemoInputModel _model = new DemoInputModel
{
PropertyOne = "Test",
PropertyTwo = "42",
PropertyThree = "Math.PI",
};

private String _selectedValidationProperty;

public String SelectedValidationProperty
{
get => _selectedValidationProperty;
set
{
_selectedValidationProperty = value;
ChangeValidator(value);
}
}

public Expression<Func<String>> ValidationResolver { get; set; }

protected override void OnInitialized()
{
SelectedValidationProperty = "1";
base.OnInitialized();
}

public void ChangeValidator(String value)
{
switch (value)
{
case "1":
ValidationResolver = () => _model.PropertyOne;
break;
case "2":
ValidationResolver = () => _model.PropertyTwo;
break;
case "3":
ValidationResolver = () => _model.PropertyThree;
break;
default:
break;
}
}
}

你的意思是这样的吗?如果您的模型不只有字符串,就像示例中那样,它会变得稍微复杂一些。 “快速”解决方法可能是使用 Expression对于每种可能的类型。
在幕后,该表达式用于创建 FieldIdentifier . FieldIdentifier然后用于从 EditContext 中获取相应的属性/字段检查验证状态。因此,您在为表达式选择什么方面受到限制。错误消息 FieldIdentifier only supports simple member accessors (fields, properties) of an object 很好地说明了这一限制。

关于c# - 如何在 Blazor 中动态设置 ValidationMessage<TValue>.For 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65393962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com