Asp.net mvc HttpPostedFileBase中的null值[相同名称,在模式弹出窗口中显示的partialview中的正确enctype]
在模式中打开的局部视图Asp.net mvc HttpPostedFileBase中的null值[相同名称,在模式弹出窗口中显示的partialview中的正确enctype],asp.net-mvc,razor,file-upload,Asp.net Mvc,Razor,File Upload,在模式中打开的局部视图 @using (Html.BeginForm("Create", "Line", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() @Html.LabelFor(model => model.U_Attachment, htmlAttributes: new { @class = "control-label col-md-2"
@using (Html.BeginForm("Create", "Line", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.LabelFor(model => model.U_Attachment, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.U_Attachment, new { htmlAttributes = new { @type = "file" } })
@Html.ValidationMessageFor(model => model.U_Attachment, "", new { @class = "text-danger" })
}
行动
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(LINE model, HttpPostedFileBase U_Attachment)
{
//value is null for U_Attachment
}
我确认enctype是正确的,方法是post,名称是正确的。这里还缺什么
更新:要加载弹出模式,我使用以下脚本。此脚本将重新加载带有结果url的div。这就是问题所在吗
**
**$(function () {
$.ajaxSetup({ cache: false });
$("a[data-modal]").on("click", function (e) {
$('#myModalContent').load(this.href, function () {
$('#myModal').modal({
/*backdrop: 'static',*/
keyboard: true
}, 'show');
bindForm(this);
});
return false;
});
});
function bindForm(dialog) {
$('form', dialog).submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
if (result.success) {
$('#myModal').modal('hide');
$('#replacetarget').load(result.url); // Load data from the server and place the returned HTML into the matched element
} else {
$('#myModalContent').html(result);
bindForm(dialog);
}
}
});
return false;
});
}
</script>**
**
**$(函数(){
$.ajaxSetup({cache:false});
$(“a[数据模式]”)。在(“点击”按钮上,函数(e){
$('#myModalContent').load(this.href,函数(){
$(“#myModal”).modal({
/*背景:“静态”*/
键盘:正确
}"show";;
bindForm(本);
});
返回false;
});
});
函数绑定窗体(对话框){
$('form',dialog).submit(函数(){
$.ajax({
url:this.action,
类型:this.method,
数据:$(this).serialize(),
成功:功能(结果){
如果(结果、成功){
$('#myModal').modal('hide');
$('#replacetarget').load(result.url);//从服务器加载数据,并将返回的HTML放入匹配的元素中
}否则{
$('#myModalContent').html(结果);
bindForm(对话框);
}
}
});
返回false;
});
}
**
1.如果您想以ajax模式将表单数据和文件发送到服务器,您应该使用ajaxForm库
因此,您应该在cshtml页面中引用ajaxForm.js库
$('form').ajaxForm();
请写$('form').ajaxForm()代码>在jquery的主函数之前
比如说
<script>
$('form').ajaxForm();
$(function(){
//write your normal code
});
</script>
$('form').ajaxForm();
$(函数(){
//编写您的普通代码
});
2.脚本代码无法在局部视图中工作
因此,将js脚本移动到一个主页面,该主页面是此部分视图的父级使用ajax上传文件需要:
dataType: "json",
processData: false,
contentType: false,
@using (Html.BeginForm("Create", "WebPRLine", FormMethod.Post, new { enctype = "multipart/form-data" , id = "CreateLine"}))
{
}
作为$.ajax构造函数的一部分。以下是对我有效的解决方案(我以前也遇到过同样的问题):
-使用普通html文件输入,不使用html帮助程序,并添加属性“name”
[HttpPost]
[ValidateAntiForgeryToken]
公共操作结果创建(行模型、HttpPostedFileBase U_附件)
{
//U_附件的值为空
}
@斯蒂芬帮了我的忙
为了让事情更清楚,,
我在局部视图中为我的表单提供了一个ID
@using (Html.BeginForm("Create", "WebPRLine", FormMethod.Post, new { enctype = "multipart/form-data" , id = "CreateLine"}))
{
}
然后更新Ajax调用,如下所示,该调用发送表单数据和文件
function bindForm(dialog) {
$('form', dialog).submit(function () {
var formdata = new FormData($('#CreateLine').get(0));
$.ajax({
url: this.action,
type: this.method,
data: formdata,
processData: false,
contentType: false,
success: function (result) {
if (result.success) {
$('#myModal').modal('hide');
$('#replacetarget').load(result.url); // Load data from the server and place the returned HTML into the matched element
} else {
$('#myModalContent').html(result);
bindForm(dialog);
}
}
});
return false;
});
}
谢谢大家。干杯 你能签入请求文件吗?是的,没有。你的附件是否包含在C_SI7_WEBPRLINE模型中,如果是,在操作中是否正确引用了?此外,还需要检查模态是否创建了“表单中的表单”?U_附件在C_SI7_WEBPRLINE中。我检查了生成的HTML,它也有正确的名称。使用.serialize()
对文件输入不起作用。您可以使用FormData
使用ajax上传文件。在页面中重新引用ajaxForm库后,应添加$('form')。ajaxForm();到你的代码