Как динамически привязать значение для свойств поля? - БлазорC#

Место общения программистов C#
Ответить
Гость
 Как динамически привязать значение для свойств поля? - Блазор

Сообщение Гость »


Я хочу динамически отображать свойства поля в EditForm, но синтаксис не подходит для

Код: Выделить всё

@bind-Value="@(field.Property as string)"
I get

Код: Выделить всё

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
. Any idea how I can achieve that?

Код: Выделить всё

@if (model != null)
{
foreach (ModelField field in model.GetFields())
{

@field.Name

@switch (field.Type)
{
case FieldType.Text:


break;
}

}
}


private IModel? model;

protected override void OnParametersSet()
{
base.OnParametersSet();

switch (Input)
{
case "test1":
model = new Test1();
break;
case "test2":
model = new Test2();
break;
}
}

Код: Выделить всё

    public enum FieldType
{
Text,
Number,
}

public class ModelField
{
public object Property { get; }
public string Name { get; }
public FieldType Type { get; }

public ModelField(object property, string name, FieldType type)
{
Property = property;
Name = name;
Type = type;
}
}

Код: Выделить всё

    public class Test1 : IModel
{
[Required]
public int Phone { get; set; }
[Required]
public string Message { get; set; }

public IEnumerable GetFields()
{
return new List
{
new ModelField(Phone, "Phone", FieldType.Number),
new ModelField(Message, "Message", FieldType.Text)
};
}
}

Код: Выделить всё

    public interface IModel
{
IEnumerable GetFields();
}



Источник: https://stackoverflow.com/questions/781 ... ies-blazor
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»