MudBlazor MudSelect: Unable to set property 'IMudShadowSelect' on object of type 'MudBlazor.MudSelectItem`1

269 Views Asked by At

I'm trying to populate a mudselect with a list of items, and bind the selected value to an object to create a new object. I'm probably not explaining that very well but I've been unable to find examples of how to do something like this. Everything in the MudBlazor documentation is dirt simple hard coded lists of values, and everything coming up when I google seems way more complex than what I'm trying to do. This should be simple... or so I thought.

I've tried 20 different combinations for the MudSelect and even though everything seems right to me, I keep getting this error:

Unable to set property 'IMudShadowSelect' on object of type 'MudBlazor.MudSelectItem1 The error was: Unable to cast object of type 'MudBlazor.MudSelect1[System.String]' to type 'MudBlazor.MudSelect`1[System.Int32]'.

I Dont understand where its trying to convert a string to an int. I want it to return an int! Originally I was trying to make it as simple as possible and only use a string for both the value and the text, but I was getting this error so I changed my model to include an int as the Id and I'm still getting the problem. Ready to throw my computer out the window. I feel like this really shouldnt be this hard.. I have to be missing something. I've spent like 5 hours searching, trying things, ChatGPT, everything. So I come here asking for help as a last resort.

My Code (or at least one of the 15 different ways I've tried to do this):

            <MudSelect Label="Select a category" @bind-Value="@newTrans.CategoryId">
                @foreach (var category in categories)
                {
                    <MudSelectItem Value="@category.Id">@category.Name</MudSelectItem>
                }
            </MudSelect>

    public class CategoryModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

    public class TransactionModel
    {
        public string UserId { get; set; }
        public Guid TransId { get; set; }
        public string? Description { get; set; }
        public decimal? Amount { get; set; }
        public DateTime? TransDate { get; set; }  
        public DateTime CreatedTS { get; set; }
        public string? Type { get; set; }    
        
        public int CategoryId { get; set; }
        public string? CategoryName { get; set; }
     }


This compiles fine and seems like it should work according to what little info I've found online. But I get the above error.

Also isn't there an easier way to do this without building the list of by using the Items property of the MudSelect? I tried that too of course and was getting the same error.

Any help or a link to an example of how I'd do something like this would be greatly appreciated.

Thanks!

1

There are 1 best solutions below

0
On

I just had the same issue.

With

 <MudSelect T="int?" ...
   <MudSelectItem T="int"

This throws the "..'IMudShadowSelect' on object of type 'MudBlazor.MudSelectItem`1.." exception.

Making the MudSelectItem Type the same as the Select fixed it.

 <MudSelect T="int?" ...
   <MudSelectItem T="int?"