在MVC视图中,我需要将模型中的值从KendoUI Widget传递到控制器。
问题描述:
下面的代码可用。当执行上传控件时,控制器会收到“测试值”值。在MVC视图中,我需要将模型中的值从KendoUI Widget传递到控制器。
但是,我从该视图中显示的模型中获取了需要发送的关键值,而不是硬编码文本。
注意。这是一个网格弹出编辑器中的自定义模板。
Widget ViewBag.Title =“Test Value”;
@(Html.Kendo().Upload()
.Name("files")
.TemplateId("fileTemplate")
.Async
(a => a
.Save("Save", "OpenRecords"), new { MyRequest = ViewBag.Title })
.AutoUpload(true))
)
控制器
public ActionResult Save(IEnumerable<HttpPostedFileBase> files, string MyRequest)
{
// The Name of the Upload component is "files"
if (files != null)
{
foreach (var file in files)
{
// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = Path.GetFileName(file.FileName);
var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);
// The files are not actually saved in this demo
file.SaveAs(physicalPath);
ViewBag.FileName = fileName;
//return Content(physicalPath);
}
}
// Return an empty string to signify success
return Content("");
}
答
控制范围内使用 '上传' 事件。
@(Html.Kendo().Upload()
.Name("files")
.TemplateId("fileTemplate")
.Events(e => e.Upload("onFileUpload"))
.Async(a => a.Save("Save", "Attachment").AutoUpload(true)))
的在你的JavaScript,定义该方法onFileUpload:
var onFileUpload = function (e) {
e.data = { MyRequest : ViewBag.Title };
}
希望这有助于。祝你好运