New to Telerik UI for ASP.NET Core? Start a free 30-day trial
Sending a Model Property to the Server When Removing a File from the Upload
Environment
Product | Telerik UI for ASP.NET Core Upload |
Progress Telerik UI for ASP.NET Core version | Created 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
- 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);
}
}
})
)
- Handle the
Remove
event of the Upload that triggers when an uploaded file is about to be removed. Get theId
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"))
...
)