所以我有 ajax 文件上传部分(使用Jquery Form插件),它工作得很好,但我不知道在文件上传后更新模型值
<div>
@Html.Partial("PhotoUpload", Model.Place)
</div>
在这里,我称其为 partial 并将其作为模型的一部分。
@model PlaceMap.DAL.Entities.Place
@using (Html.BeginForm("PhotoUpload", "Place", FormMethod.Post, new { @id = "photoUpload", enctype = "multipart/form-data" }))
{
{
@Html.ValidationSummary(true, "Image upload was unsuccessful")
@Html.HiddenFor(m => m.Photo)
<input type="file" id="file" name="file"/>
<input type="submit" id="sbm" />
}
}
这是部分视图代码,接受模型和上传表单
var options = {
url: "/Place/PhotoUpload",
dataType: "json",
clearForm: true,
resetForm: true,
success: showResponse
};
function showResponse(responseText, statusText, xhr, $form)
{
$('#photo').append('<img src="/Images/Places/' + responseText.Photo + '" />');
}
$('#photoUpload').submit(function ()
{
$('#photoUpload').ajaxSubmit(options);
return false;
});
插件的Javascript代码
[Authorize]
[HttpPost]
public ActionResult PhotoUpload(string Photo, HttpPostedFileBase file)
{
try
{
using (var ms = new MemoryStream())
{
//some logic here
return Json(new { Photo = filename });
}
}
catch (ArgumentException)
{
}
return PartialView();
}
控制器操作代码。它返回文件名,它将转到 js 函数“showResponse”并将图像附加到 div。这一切都很好,但我必须将文件名写入这个部分的@Model.Photo,我不知道该怎么做。有什么建议么?