Динамические компоненты бритвыC#

Место общения программистов C#
Anonymous
Динамические компоненты бритвы

Сообщение Anonymous »

ОК, поэтому я пытаюсь создать несколько общих компонентов, но у меня возникают некоторые проблемы, передающие значение ASPFOR в.public class InputModel : ComponentModel
{
public string AspFor { get; set; }
public string Value { get; set; }
public string? Disabled { get; set; }
}
public class ComponentModel
{
public string Id { get; set; }
public string Label { get; set; }
public bool IsMandatory { get; set; }
public ComponentEnum Componente { get; set; }
}
< /code>
public enum ComponentEnum
{
Input
}

public static class ComponentEnumExtensions
{
public static string GetPath(this ComponentEnum component)
{
return component switch
{
ComponentEnum.Input => "*Path_To_Input.cshtml*"
_ => string.Empty
};
}
}

< /code>

@(Model.Label)
@if (Model.IsMandatory)
{
(Mandatory)
}
@Model.Label


< /code>
i am then creating an instance of the input model and rendering it in the page with this model:
public class RegistoViewModel
{
public Contactos Contactos { get; set; }
}

public class Contactos
{
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string Email { get; set; } = string.Empty;
}
< /code>
@{
var input = new InputModel
{
Id = "inputId",
Label = "Email",
Componente = ComponentEnum.Input,
AspFor = "Contactos.Email",
Value = Model.Contactos.Email
};
}

@await Html.PartialAsync(ComponentEnum.Input.GetPath(), input)
< /code>
can someone help me understand how can i pass the AspFor dynamicaly and be able to create a generic InputModel
when i create the Div directly in the page it works ok:


@(Model.Test.Label)
@if (Model.Test.IsMandatory)
{
(Obrigatório)
}
Email



< /code>
public class RegistoViewModel
{
public Contactos Contactos { get; set; }
public InputModel Test{ get; set; }
}

public class Contactos
{
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string Email { get; set; } = string.Empty;
}


Подробнее здесь: https://stackoverflow.com/questions/795 ... components

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