我有个问题。我使用 Kendo UI for ASP.NET MVC 为我的 Web 应用程序创建上传功能。它运作良好,但问题是用户在单击上传按钮执行提交表单并导航到其他页面后看不到他们之前上传的文件。在这种情况下,我在创建视图中放置了一个上传器,我希望在将文件上传到服务器后,将该文件的链接存储到数据库中我的模型的一个字段中,但它总是得到空值。这里有人可以给我一些建议吗?太感谢了。这是我使用的代码。* 控制器:
[HttpPost]
public ActionResult Create(Model model)
{
if(ModelState.IsValid)
{
model.Url = Url;
Db.Models.Add(model);
Db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments)
{
// The Name of the Upload component is "attachments"
foreach (var file in attachments)
{
// Some browsers send file names with full path. We only care about the file name.
var fileName = Path.GetFileName(file.FileName);
var destinationPath = Path.Combine(Server.MapPath("~/Contents/files/"), fileName);
DestinationPath = destinationPath;
file.SaveAs(destinationPath);
}
// Return an empty string to signify success
return Content("");
}
看法
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="editor-label">
Upload file
</div>
<div style="width:45%">
@Html.Kendo().Upload().Name("attachments").Async(async => async.Save("Save", "Model").AutoUpload(true)).Multiple(false)
</div>
<p>
<input type="submit" value="Create" class="k-button" />
</p>
模型
public string Url {get;set;}