任何人都可以告诉我如何在控制器方法中检索formData 在ASP.net MVC?
<script type="text/javascript">
$(function () {
$('#fileupload').fileupload({
contentType: 'application/json; charset=utf-8',
url: '/Home/GoodSave'
})
.on("fileuploadsubmit", function (e, data) {
data.formData = {
jsonOfLog: $("#ddlDocumentType option:selected").text()
};
});
$("#fileuploadbutton").on("click", function() {
$("#fileupload").submit();
});
});
</script>
答案 0 :(得分:1)
您通常在控制器操作中指定一个参数,其名称与表单数据中的字段相同:
[HttpPost]
public ActionResult GoodSave(string jsonOfLog)
或者使用相应的字段定义模型:
public class Form {
public string jsonOfLog {get; set;}
}
[HttpPost]
public ActionResult GoodSave(Form data)
答案 1 :(得分:0)
我认为您正在尝试使用ajax上传文件。 使用this link通过ajax上传文件。
但是,下面是获取发布的表单数据的示例。
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
答案 2 :(得分:0)
感谢U10和Jefraim Ngek,从你的答案中找到线索,我就这样做了:
在视图中:
<script type="text/javascript">
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
url: '/Home/GoodSave', maxFileSize: 5000000,
add: function (e, data) {
$("#fileuploadbutton").click(function () {
data.formData = { jsonOfLog: $("#ddlDocumentType option:selected").text() };
data.submit();
});
},
});
});
</script>
在Controller方法中:
[HttpPost]
public JsonResult GoodSave(string jsonOfLog)
{