Bind integer to DropDown, but as a string
Good day, I have a question relating to Sf DropDown List component. I have this code right now:
<SfDropDownList ID="productType" TValue="string[]" TItem="ProductTypes" DataSource="@ProductVals" @bind-Value="@defaultVal" Placeholder="Product Type">
<DropDownListTemplates>
<ItemTemplate Context="productType">
<option value="@((int)ProductTypes.Regular)">@(ProductTypes.Regular.ToString())</option>
<option value="@((int)ProductTypes.Service)">@(ProductTypes.Service.ToString())</option>
</ItemTemplate>
</DropDownListTemplates>
</SfDropDownList>
<ValidationMessage For="() => product.ProductType" />
And this c# that I am trying to bind to:
public int ProductType { get; set; } = (int)ProductTypes.Regular; // integer
public string[] ProductVals = Enum.GetNames(typeof(ProductTypes));
public ProductTypes defaultVal { get; set; } = ProductTypes.Regular;
public enum ProductTypes
{
Regular = 1,
Service = 2,
Replacement = 3,
Group = 4,
}
Yet this method is throwing a multitude of errors, like "cannot convert type to string", and "cannot convert lambda expression to type", etc. Any help would be greatly appreciated. Please note I only wish to bind the first two values from PublicTypes, as there are two options.
SIGN IN To post a reply.
3 Replies
1 reply marked as answer
PO
Prince Oliver
Syncfusion Team
December 1, 2021 01:19 PM UTC
Hi
Bryce,
Thank you for contacting us.
To bind Enum to the DropDownList component please refer to the following documentation.
We have checked the shared code snippet. Please make the following changes to your code.
|
<SfDropDownList ID="productType" TValue="ProductTypes" TItem="string" DataSource="@ProductVals" @bind-Value="@defaultVal" Placeholder="Product Type"> SfDropDownList> |
|
public int ProductType { get; set; } = (int)ProductTypes.Regular; // integer
public string[] ProductVals = Enum.GetNames(typeof(ProductTypes)).Take(2).ToArray();// To select only 2 items
public ProductTypes defaultVal { get; set; } = ProductTypes.Regular;
public enum ProductTypes
{
Regular = 1,
Service = 2,
Replacement = 3,
Group = 4,
} |
Please let us know if you need any further assistance on this.
Regards,
Prince
Marked as answer
BS
Bryce Steuer
December 1, 2021 09:47 PM UTC
Hello, thank you for your help. One more question: how to convert this type of dropdown to SyncFusion?
<SfDropDownList ID="productPackBreak" @bind-Value="@product.PackBreakParent" Placeholder="Pack Break Parent"> *@
@* <DropDownListTemplates> *@
@* <ItemTemplate Context="productType"> *@
@* <option value="0">(None)</option> *@
@* @foreach (var p in ProductsWithoutPackChild) *@
@* { *@
@* <option value="@p.Id">@p.LookupCode - @p.Description</option> *@
@* } *@
@* </ItemTemplate> *@
@* </DropDownListTemplates> *@
@* </SfDropDownList> *@
public int? PackBreakParent { get; set; }
PackBreakParent is not an enum.
ProductBases = PosDb.ProductBases.OrderBy(i => i.Description).ToList();
ProductsWithoutPackChild = (from p in PosDb.Products
from pParent in PosDb.Products.LeftJoin(i => i.PackBreakParent == p.Id)
where p.ProductType == (int)ProductTypes.Regular &&
((product.Id > 0 && p.Id != product.Id && pParent == null) || product.PackBreakParent == p.Id ||
(product.Id == 0 && pParent == null))
orderby p.Description
select p).ToList();
ProductsForReplace = PosDb.Products.Where(i =>
i.ProductType == (int) ProductTypes.Regular && i.Id != product.Id && !i.IsDisabled)
.OrderBy(i => i.Description).ToList();
BC
Berly Christopher
Syncfusion Team
December 2, 2021 12:14 PM UTC
Hi Bryce,
In the shared screenshot, you have rendered the Syncfusion DropDownList. So, please elaborate the requirement which help us to check and provide prompt solution from our end. Else, share the issue reproducing sample that will help us to check and proceed further from our end.
Regards,
Berly B.C
SIGN IN To post a reply.
- 3 Replies
- 3 Participants
- Marked answer
-
BS Bryce Steuer
- Nov 30, 2021 05:38 PM UTC
- Dec 2, 2021 12:14 PM UTC