Cast that ViewBag variable first.
.Enable(bool.Parse((string)ViewBag.AllowEdit))
Question
I need find a way to dynamically enable or disable a Kendo upload:
@(Html.Kendo().Upload()
.Enable(false)
.Name("attachments_" + item.QuestionId)
.ShowFileList(true)
.TemplateId("fileTemplate")
.Async(a => a
.Save("SaveAttachment", "Attachment", new { evaluationId = ViewBag.EvaluationId, questionId = item.QuestionId })
.Remove("RemoveAttachment", "Attachment", new { evaluationId = ViewBag.EvaluationId, questionId = item.QuestionId })
.AutoUpload(true)
)
.Files(files =>
{
if ((IList<dynamic>)ViewData["Attachment_" + item.QuestionId] != null)
{
foreach (var f in (IList<dynamic>)ViewData["Attachment_" + item.QuestionId])
{
files.Add().Name(f.Name).Extension(f.Extension).Size(f.Size);
}
}
})
)
How can I do that?
I tried setting .Enable to:
.Enable(bool.Parse(ViewBag.AllowEdit))
And it threw an error:
Server Error in '/' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Source Error:
Line 95: .ShowFileList(true) Line 96: .TemplateId("fileTemplate") Line 97: .Async(a => a Line 98: .Save("SaveAttachment", "Attachment", new { evaluationId = ViewBag.EvaluationId, questionId = item.QuestionId }) Line 99: .Remove("RemoveAttachment", "Attachment", new { evaluationId = ViewBag.EvaluationId, questionId = item.QuestionId })
Is there a simpler way to do this?
Solution
Cast that ViewBag variable first.
.Enable(bool.Parse((string)ViewBag.AllowEdit))
OTHER TIPS
One thing to be aware of with Kendo upload control is that even when it is disabled users can still drag and drop files on to it. There is a way to always have drag and drop disabled: kendo forum
I did not try that workaround since I want to allow it when the control is enabled.