每页有多个 Telerik MVC 上传控件?

发布于 2024-12-28 21:53:41 字数 820 浏览 0 评论 0原文

我正在使用 Telerik Extensions for ASP.NET MVC 3,但我在上传控件方面遇到问题。我想在同一页面上有多个上传控件(例如,我要求提供图像和声音文件)。问题是,只有第一个上传控件有效。页面上的任何其他内容都呈现良好,单击它们仍然会弹出文件选择对话框,但除了第一个之外,没有任何内容真正响应。

Telerik 似乎没有报告控制台中的任何错误或任何其他内容,它只是不起作用。我是否需要做一些特定的事情才能在一个页面上允许多个上传控件?

为了演示,这是不起作用的损坏代码:

@(Html.Telerik().Upload()
    .Name("attachments")
    .Async(async => async.Save("Save", "Resource").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemImageOnSuccess")
        .OnError("ItemImageOnError")
    )
)

@(Html.Telerik().Upload()
    .Name("attachments")
    .Async(async => async.Save("Save", "Resource").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemSoundOnSuccess")
        .OnError("ItemSoundOnError")
    )
)

第二个上传字段已正确呈现,但不起作用。

I'm using the Telerik Extensions for ASP.NET MVC 3 and I'm having a problem with the Upload control. I would like to have multiple upload controls on the same page (I'm asking for an image and a sound file, for example). The problem is, only the first Upload control works. Any others on the page render fine, and clicking on them still brings up the file selection dialog, but none actually responds except the first one.

Telerik doesn't seem to be reporting any errors in the console or anything, it just simply doesn't work. Is there something specific I have to do to allow multiple upload controls on one page?

To demonstrate, this is the broken code that doesn't work:

@(Html.Telerik().Upload()
    .Name("attachments")
    .Async(async => async.Save("Save", "Resource").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemImageOnSuccess")
        .OnError("ItemImageOnError")
    )
)

@(Html.Telerik().Upload()
    .Name("attachments")
    .Async(async => async.Save("Save", "Resource").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemSoundOnSuccess")
        .OnError("ItemSoundOnError")
    )
)

The second upload field is rendered properly, but does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

篱下浅笙歌 2025-01-04 21:53:41

对于遇到类似问题的其他人,我自己设法解决了。
我的问题是页面上的所有上传控件都具有相同的名称,这反过来又为它们提供了相同的 id,因此 Telerik 的所有 javascript 只能在第一个上传控件上工作。解决方法很明显:更改名称。

然而,这引入了一个新问题,因为我使用的所有上传控件都指向相同的函数,并且在更改控件名称时,参数不再正确映射,我只是得到一个空文件列表。为了解决这个问题,我需要向 .Save() 函数添加一个额外的参数来告诉它字段名称。

这是我上面代码的修复:

@(Html.Telerik().Upload()
    .Name("ImageUpload")
    .Async(async => async.Save("Save", "Resource", "attachments").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemImageOnSuccess")
        .OnError("ItemImageOnError")
    )
)

@(Html.Telerik().Upload()
    .Name("SoundUpload")
    .Async(async => async.Save("Save", "Resource", "attachments").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemSoundOnSuccess")
        .OnError("ItemSoundOnError")
    )
)

For anyone else who comes across a similar issue, I managed to solve it myself.
My issue was that I had all upload controls on the page with the same name, which in turn gave them the same id, so all of Telerik's javascript would only work on the first Upload control. The fix is obvious: Change the names.

However, this introduced a new issue, because all the Upload controls I was using pointed to the same function, and in changing the name of the control, the parameter did not map correctly any more and I was just getting a null file list. To fix this issue, I needed to add an extra parameter to the .Save() function to tell it the field name.

Here is the fix for my code above:

@(Html.Telerik().Upload()
    .Name("ImageUpload")
    .Async(async => async.Save("Save", "Resource", "attachments").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemImageOnSuccess")
        .OnError("ItemImageOnError")
    )
)

@(Html.Telerik().Upload()
    .Name("SoundUpload")
    .Async(async => async.Save("Save", "Resource", "attachments").AutoUpload(true))
    .ClientEvents(events => events
        .OnSuccess("ItemSoundOnSuccess")
        .OnError("ItemSoundOnError")
    )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文