**Jquery Ajax call sending data to controller**
$("#btncareerssubmimt").click(function () {
var Name = $("#txtname").val();
var file = $("#fileresume").val();
$.ajax({
type: "POST",
url: '/Footer/sendmail',
data: { Name: Name ,file: file },
success: function () {
$("#simplecareers").html('<p style="color:#74ac12;margin-left:19%;">Your Request Submitted Successfully</p>');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#simple-msg").html('<pre>"fail"</pre>');
}
});
}
在上面的ajax调用我正确地获取名称和文件路径但是当我们传递给控制器时 仅获取名称,但文件路径变为空我的Html视图
<input type="text" name="txtname" id="txtname" class="form-control" value=""/>
input type="file" name="file" id="fileresume" value=""/>
<input type="submit" value="Upload" id="btncareerssubmimt" class="submit" />
This is my controller
[HttpPost]
public ActionResult sendmail(string Name ,HttpPostedFileBase fileresume)
{
}
答案 0 :(得分:2)
将您的HTML更改为:
<form id="formToSendEmail" action="/Footer/sendmail">
<input type="text" name="Name" id="txtname" class="form-control" value=""/>
<input type="file" name="fileresume" id="fileresume" value=""/>
<input type="button" value="Upload" id="btncareerssubmimt" />
</form>
和js:
$("#btncareerssubmimt").click(function () {
$("#formToSendEmail").ajaxSubmit({
type: "POST",
success: function () {
$("#simplecareers").html('<p style="color:#74ac12;margin-left:19%;">Your Request Submitted Successfully</p>');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#simple-msg").html('<pre>"fail"</pre>');
}
});
}
.ajaxSubmit()
是来自jQuery Form Plugin的方法,它有助于管理表单提交过程 - jQuery Form Plugin。