Dynamische RasierkomponentenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Dynamische Rasierkomponenten

Post by Anonymous »

Ok, also versuche ich, einige generische Komponenten zu erstellen, aber ich habe einige Probleme, die den ASPFOR -Wert in der .cshtml < /p>
übergeben habe. Ich habe ein Eingabemodell und das teilweise < /p>
erstellt

Code: Select all

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;
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post