Help SfDropDownList Trap Selected Value
After lots of searching I just found out that the SfDropDownList default value is 0. No wonder my DataAnnotationsValidator did not detect any errors.
I was wondering is there a way or work around to trap SfDropDownList if selected value is 0? I don't want my form to submit until the user selects an item from the list.
SfDropDownList DataSource="@supplier"
TItem="Supplier" TValue="int" Text="SupplierID" @bind-Value="addeditProduct.ProductSupplierID" FloatLabelType="@FloatLabelType.Always" Enabled="true" Placeholder="Supplier" AllowFiltering="true" FilterBarPlaceholder="Filter Suppliers" CssClass="e-custom"> DropDownListFieldSettings Text="SupplierName" Value="SupplierID"> /SfDropDownList> ValidationMessage For="@(() => addeditProduct.ProductSupplierID)" />
public class Product { [Required] public int ProductID { get; set; } [Required(ErrorMessage = "Product Code is a Required Field." )] [StringLength(25)] public string ProductCode { get; set; } [Required(ErrorMessage = "Product Description is a Required Field.")] [StringLength(50)] public string ProductDescription { get; set; } [Required(ErrorMessage = "Unit Price is a Required Field.")] [Min(1,ErrorMessage = "Minimum value must be > 0.")] public decimal ProductUnitPrice { get; set; } [Required(ErrorMessage = "Supplier is a Required Field.")] public int ProductSupplierID { get; set; } [Required] public bool ProductIsArchived { get; set; } public string SupplierName { get; set; } }For now I'm using the old way of validating things (code below). I wish I could use the ValidationMessage funtionality of Blazor so that my UI will be consistent.
public bool ValidateFields()
{
if (addeditProduct.ProductSupplierID == 0)
{
return false;
}
else
{
return true;
}
}
Thanks in advance.
SIGN IN To post a reply.
1 Reply
1 reply marked as answer
JM
Jeyanth Muthu Pratheeban Sankara Subramanian
Syncfusion Team
April 19, 2021 11:59 AM UTC
Hello Oliver,
Greetings from Syncfusion support.
We checked the reported query. We would like to let you know you that if you have rendered the Dropdownlist with TValue as int, then it will be rendered with default value of the int type(0) in C#. Therefore, Dropdownlist is rendered with a value and so it allows to submit form. In order to handle this, we suggest you to use TValue as int?, to resolve this issue. We made a sample for your convenience. Please find the sample in the below link.
Sample Link : https://www.syncfusion.com/downloads/support/directtrac/general/ze/Dropdownlist_Validation55087238
Code Snippet :
|
<EditForm Model="Model">
<DataAnnotationsValidator></DataAnnotationsValidator>
<SfDropDownList ID="Assignee" Placeholder="Select Supplier" TItem="MemberModel" TValue="int?" DataSource="@Members" @bind-Value="@Model.Assignee">
<DropDownListFieldSettings Text="Name" Value="Id"></DropDownListFieldSettings>
</SfDropDownList>
<ValidationMessage For="@(() => Model.Assignee)" />
<input type="submit" class="e-btn" />
</EditForm>
|
Screenshot :
|
|
Kindly integrate the provided solution with your application and get back to us if you need any further assistance on this.
Regards,
Jeyanth.
Marked as answer
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
- Marked answer
-
OT oliver tejada
- Apr 16, 2021 06:29 PM UTC
- Apr 19, 2021 11:59 AM UTC