How to edit and remove item from IReadonlyList in Blazor?

RAMAN RAGAVAN 46 Reputation points

I have two lists having uploaded image details here-:

list 1- IReadOnlyList<BrowserFile>? ListFileToUpload = Array.Empty<BrowserFile>(); ---binding to---> <MudList> list 2- IReadOnlyList<Images>? imageListUI { get; set; } = Array.Empty<Images>(); ---binding to---> <MudTable>

one list for pure image/uploaded file metadata and second list also contains same but for different operatios later.whenever user browse multiple images, first list will get the uploaded file details and trigger the second list..First list details i am binding in a <MudList> and second list i am binding first list in <MudList> and second list in <MudTable> .Both <MudList> and <MudTable> are in different component . second list getting details automaticaly when first list got updated.Till this i am able to do but for next, from <MudTable>,(using only second list) i need to edit the file name and i need a remove file items.that shoud reflect both <MudList> and <MudTable>. How i can do this edit and remove item operation from second list this with the below code pattern


  &lt;FileInput @bind-SelectedFiles=&#34;createimage.ListFileToUpload&#34;/&gt; //calling fileinput component by passing file 

 //in below table i need to add inline edit and delete item functionality.that change should reflects in  &lt;MudList&gt;
   @if (createimage.imageList.Count &gt; 0)

            &lt;MudTable Items=&#34;createimage.imageListUI&#34; Hover=&#34;true&#34; Dense=&#34;true&#34; Class=&#34;mb-4&#34; Striped=&#34;true&#34;&gt;
            &lt;RowTemplate Context=&#34;mediaItem&#34;&gt;
                    &lt;MudTd DataLabel=&#34;Filename&#34;&gt;@mediaItem.Name&lt;/MudTd&gt;
                    &lt;MudTd DataLabel=&#34;FileNameWithoutExtension&#34;&gt;@mediaItem.ContentType&lt;/MudTd&gt;
                &lt;MudTd DataLabel=&#34;Edit&#34;&gt;

 public partial class UI

   private Createimage createimage = new();

//initializing and get set to lists

     public class Createimage{

    public IReadOnlyList&lt;Images&gt;? imageListUI { get; set; } = Array.Empty&lt;Images&gt;();

    private IReadOnlyList&lt;BrowserFile&gt;? _ListFileToUpload = Array.Empty&lt;BrowserFile&gt;();

    public IReadOnlyList&lt;BrowserFile&gt;? ListFileToUpload

        get { return this._ListFileToUpload; }

            this._ListFileToUpload = value;
            imageListUI = value.Select(file =&gt; new Images(file)).ToList();


  public class Images
    private BrowserFile file;

    public Images(BrowserFile file)
        this.file = file;
        Name = file.FileName;
        ContentType = file.FileExtension;
    public String Name { get; set; }
    public String ContentType { get; set; }

 public class BrowserFile 

 public BrowserFile(IBrowserFile file)
    File = file ?? throw new ArgumentNullException(nameof(file));
    FileName = Path.GetFileName(File.Name);

public IBrowserFile File { get; }



                @foreach (var fileInfo in SelectedFiles)
                    &lt;MudListItem @key=&#34;@fileInfo.File&#34;&gt;
                        &lt;MudChip Color=&#34;Color.Dark&#34;
                                 Style=&#34;overflow: hidden; width: 60px;&#34;

public IReadOnlyList&lt;BrowserFile&gt; SelectedFiles { get; set; } = Array.Empty&lt;BrowserFil&gt;(); //getting selected files 
through component parameter

private async Task OnFilesChanged(InputFileChangeEventArgs e) {

   var files = new List&lt;BrowserFile&gt;(e.FileCount);
    files.AddRange(e.GetMultipleFiles(50).Select(file =&gt; new BrowserFilefile)));

A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
795 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 18,881 Reputation points Microsoft Employee


    The IReadOnlyList<T> represents a list in which the number and order of list elements is read-only. So, it doesn't provide the Add/Remove method to directly update the list items.

    To add/update/remove the records in the IReadOnlyList<T>, you could convert it to a List, then call the Add/Remove method to change the items, after that reset the IReadOnlyList<T>.

    You can refer to the following sample:

        public class PublicSetReadOnlyListClass
            public IReadOnlyList<string> MyList { get; set; } = new List<string>() { "original", "original2", "original3" };
        private void RemoveItem()
            var myObject = new PublicSetReadOnlyListClass();
            //myObject3.MyList.Add("added");  // this is NOT allowed, becaure the IReadOnlyList doesn't provide the Add/Remove method.
            //conevert the IReadOnlyList to a list
            List<string> exititems = myObject.MyList.ToList(); 
            //find the removed item.
            var removeitem = "original2";
            //call the add or remove method to change the existing items.
            //update the IReadOnlyList.
            myObject.MyList = exititems;

    Then the result as below:


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,