I am doing something like this in the Blazor HTML section...
<EditForm EditContext="editContext" OnValidSubmit="SubmitQuery">
<DataAnnotationsValidator />
<Microsoft.AspNetCore.Components.Forms.ValidationSummary />
<InputRadioGroup @bind-Value="@Model.Selection">
<InputRadio Value="FindGuestDisplay.Selections[0]" /><label>@(FindGuestDisplay.Selections[0]):</label>
<br />
<label class="fglabel">First Name:</label><InputText @bind-Value="@FirstName" style="width:300px" />
<label class="fglabel">Last Name:</label><InputText @bind-Value="@LastName" style="width:300px" />
...
and in the code section I am wrapping my model setters in local properties like so....
private string FirstName { get { return Model!.FirstName ?? ""; } set { Model!.FirstName = value; } }
private string LastName { get { return Model!.LastName ?? ""; } set { Model.LastName = value; } }
I did this because I get the errors throughout the whole code if I use the Model properties directly and this limits the warning to just the setters.
Note that this code works but I want to do this the right way, warning free. By the way, I am doing this almost exactly the way that Microsoft's example for using Edit form says, so how can I get rid of this error? Is there a proper alternative I should be using?
here is my model declaration within my Blazor component (not set or passed in from outside):
FindGuestDisplay Model { get; set; }= new ();
the model gets passed into the editContext here:
protected override void OnInitialized()
{
editContext = new(Model);
editContext.OnValidationRequested += HandleValidationRequested;
messageStore = new(editContext);
}
here is my class definition:
public class FindGuestDisplay
{
[Parameter]
[StringLength(50, ErrorMessage = "Email is too long.")]
public string Email { get; set; } = "";
[Parameter]
//[Required]
[StringLength(15, ErrorMessage = "First Name is too long.")]
public string FirstName { get; set; } = "";
[Parameter]
[StringLength(25, ErrorMessage = "Last Name is too long.")]
public string LastName { get; set; } = "";
[Parameter]
[StringLength(15, ErrorMessage = "Cell Phone is too long.")]
public string CellPhone { get; set; } = "";
[Parameter]
[StringLength(15, ErrorMessage = "Home Phone is too long.")]
public string HomePhone { get; set; } = "";
[Parameter]
[StringLength(25, ErrorMessage = "Work Phone is too long.")]
public string WorkPhone { get; set; } = "";
[Parameter]
[StringLength(50, ErrorMessage = "Business Name is too long.")]
public string BusinessName { get; set; } = "";
[Parameter]
public string? BarcodeID { get; set; }
public string Selection { get; set; } = "Enter Guest Information";
public static readonly List<String> Selections =
[ "Enter Guest Information", "Find Web GuestPass", "Find Business" ];
}
Any help would be appreciated. Thanks!
From my understanding, I am doing this the proper way. Is this just a bad warning?
Here is a pattern I use:
@page "/mypage"
@if (Model == null)
{
return;
}
<div>First Name: @Model.FirstName</div> Model is not null here -> no warnings
@code
{
MyPageModel? Model {get; set;}
//Instantiate the model anywhere...
}
If the model is passed as a Parameter, I use required
keyword and [EditorRequired]
:
[Parameter, EditorRequired]
public required MyPageModel {get; set;}