New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Sending a Model Property to the Server When Removing a File from the Upload

Environment

ProductTelerik UI for ASP.NET Core Upload
Progress Telerik UI for ASP.NET Core versionCreated with the 2023.1.117 version

Description

I use the Upload component in asynchronous mode, and by using its Files() configuration, I display an initial list of files when the page loads. How can I pass a View Model property Id to the server when the user removes a file from the Upload list?

Solution

  1. Populate the Ids of the initially rendered files in hidden input elements on the same View, where the Upload component is defined.
View_HtmlHelper
  @model UserViewModel

  @foreach(var file in Model.Files)
  {
      <input id="fileID_@file.Name" type="hidden" value="@file.Id" />
  }

  @(Html.Kendo().Upload()
        .Name("files")
        .Async(a => a
            .Save("Save", "Upload")
            .Remove("Remove", "Upload")
            .AutoUpload(true)
        )
        .Files(files =>
        {
            if(Model.Files != null)
            {
                foreach (var file in Model.Files)
                {
                    files.Add().Name(file.Name).Extension(file.Extension).Size(file.Size ?? 0);
                }
            }
        })
  )
  1. Handle the Remove event of the Upload that triggers when an uploaded file is about to be removed. Get the Id of the file that will be removed from the respective hidden input, and send it to the server through the Upload Remove request.
View_HtmlHelper
  @model UserViewModel

  @foreach(var file in Model.Files)
  {
      <input id="fileID_@file.Name" type="hidden" value="@file.Id" />
  }

  @(Html.Kendo().Upload()
        .Name("files")
        .Events(ev => ev.Remove("onRemove"))
        ...
  )

More ASP.NET Core Upload Resources

See Also