Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.Below is my code structure

What I have tried:

MainUI.razor-page

 <FileInput @bind-SelectedFiles="createimage.ListFileToUpload"/> //calling fileinput component by passing file details

//in below table i need to add inline edit and delete item functionality.that change should reflects in  <MudList>
 @if (createimage.imageList.Count > 0)
{
    
                <MudTable Items="createimage.imageListUI" Hover="true" Dense="true" Class="mb-4" Striped="true">
                <HeaderContent>
                    <MudTh>Filename</MudTh>
                    <MudTh>FileNameWithoutExtension</MudTh>
                </HeaderContent>
                <RowTemplate Context="mediaItem">
                        <MudTd DataLabel="Filename">@mediaItem.Name</MudTd>
                        <MudTd DataLabel="FileNameWithoutExtension">@mediaItem.ContentType</MudTd>
                    <MudTd DataLabel="Edit">
                    </MudTd>
                </RowTemplate>
            </MudTable>
  }      

@code{
   public partial class UI

    {
       private Createimage createimage = new();
     }
}


//initializing and get set to lists

public class Createimage{

        public IReadOnlyList<Images>? imageListUI { get; set; } = Array.Empty<Images>();

        private IReadOnlyList<BrowserFile>? _ListFileToUpload = Array.Empty<BrowserFile>();

        public IReadOnlyList<BrowserFile>? ListFileToUpload
        {

            get { return this._ListFileToUpload; }

            set
            {
                this._ListFileToUpload = value;
                imageListUI = value.Select(file => 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; }
	
}
	
	
	
	fileinputComponent.razor
	
	
	 <MudList>
                    @foreach (var fileInfo in SelectedFiles)
                    {
                        <MudListItem @key="@fileInfo.File">
                            <MudChip Color="Color.Dark"
                                     Style="overflow: hidden; width: 60px;"
                                     Text="@(fileInfo.FileExtension)"/>
                            @(fileInfo.FileNameWithoutExtension)
                        </MudListItem>
                    }
                </MudList>
	
	@code{
	[Parameter]
	public IReadOnlyList<BrowserFile> SelectedFiles { get; set; } = Array.Empty<BrowserFil>(); //getting selected files through component parameter

	private async Task OnFilesChanged(InputFileChangeEventArgs e)
    {
        
		 var files = new List<BrowserFile>(e.FileCount);
        files.AddRange(e.GetMultipleFiles(50).Select(file => new BrowserFilefile)));
		
    }
	}	
Posted
Updated 10-Jan-23 11:43am

1 solution

Why use a IReadOnlyList<T> and not an IList<T> or List<T>?

IReadOnlyList<T> guards against the content of the list being altered. So you cannot modify (Add/Remove) items, only modify their content if the items allow it.

UPDATE
As mentioned above, you can not Add or Remove items from a IReadOnlyList<T>. You need to replace the list.

Here is an example:
C#
<pre>var items = new List<string>
{
    "one",
    "two",
    "three"
};

var test = new TestClass(items);
foreach (string item in test.ReadOnlyList)
{
    Console.WriteLine(item);
}
Console.WriteLine("-----");

var newItems = new List<string>(items);
newItems.Reverse();

test.SetReadonlyList(newItems);

foreach (string item in test.ReadOnlyList)
{
    Console.WriteLine(item);
}
Console.WriteLine("-----");

class TestClass
{
    public IReadOnlyList<string> ReadOnlyList { get; set; }

    public TestClass(List<string> items)
    {
        ReadOnlyList = items.ToList();
    }

    public void SetReadonlyList(List<string> items)
    {
        ReadOnlyList = items;
    }
}
 
Share this answer
 
v3
Comments
[no name] 10-Jan-23 23:14pm    
I need to maintain that IReadonly list anyway.Any possible way when onchange happens, i shpuld convert in to normal list and edit and should reflect in readonly list?
Graeme_Grant 10-Jan-23 23:59pm    
See my update to my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900