嗨我在我的控制器httppost操作中有以下代码,我试图在mvc实体框架中使用ajax在我的数据库中保存申请人详细信息
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult> SaveApplicantDetails()
{
try
{
CareerApplicant ca = new CareerApplicant();
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
ca.ResumeLink = UploadFile.UploadDocFile(file, "uploads");
}
ca.Email = Request["email"].ToString();
ca.ContactNo = Request["contact"].ToString();
ca.Name = Request["name"].ToString();
ca.CareerID = Convert.ToInt32(Request["careerid"].ToString());
db.CareerApplicants.Add(ca);
db.savechanges();
以下是我的ajax电话
$("#btnSave").click(function () {
if ($("#form1").valid()) {
var fd = new FormData();
var files = $("#flResume").get(0).files; // this is my file input in which We can select multiple files.
fd.append("name", $("#txtName").val());
fd.append("email", $("#txtEmail").val());
fd.append("contact", $("#txtContact").val());
fd.append("careerid", $(this).attr("careerid"));
for (var i = 0; i < files.length; i++) {
fd.append("Uploads" + i, files[i]);
}
alert("hit1");
$.ajax({
type: "POST",
dataType: 'json',
url: '@Url.Action("SaveApplicantDetails", "Careers")',
contentType: false,
processData: false,
data: fd.serialize(),
success: function (data) {
alert("suc called");
if (data == "Success") {
alert("suc called inner");
alert("Thanks for your intrest, You applied successfuly.");
}
else {
alert("suc failed called");
alert("Error Occured while saving data.");
}
}
})
}
else {
alert('the form is not valid');
}
});
});
当我试图在我的控制器中击中断点时它没有击中...我不知道为什么..在这里寻求帮助谢谢
答案 0 :(得分:2)
我刚刚从我的控制器动作中移除[ValidateAntiForgeryToken]并且它有效..感谢你guruprasad努力:)非常感谢
[HttpPost]
public async Task<JsonResult> SaveApplicantDetails()
{
try
{
CareerApplicant ca = new CareerApplicant();
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
ca.ResumeLink = UploadFile.UploadDocFile(file, "uploads");
}
ca.Email = Request["email"].ToString();
ca.ContactNo = Request["contact"].ToString();
ca.Name = Request["name"].ToString();
ca.CareerID = Convert.ToInt32(Request["careerid"].ToString());
db.CareerApplicants.Add(ca);
int res = await db.SaveChangesAsync();
if (res > 0)
{
string mailto = Request["email"].ToString(); /*ConfigurationManager.AppSettings["SendMailTo"].ToString();*/
new Thread(() =>
{
EmailMessage EmailMsg = new EmailMessage();
string MessageString = EmailMsg.ReadFile("MailFormat.html");
Career c = db.Careers.Where(x => x.Id == ca.CareerID).FirstOrDefault();
MessageString = MessageString.ToString().Replace("{Title}", c.Title);
MessageString = MessageString.ToString().Replace("{dateTime}", DateTime.Now.ToString());
MessageString = MessageString.ToString().Replace("{UserName}", ca.Name);
MessageString = MessageString.ToString().Replace("{Email}", ca.Email);
MessageString = MessageString.ToString().Replace("{Contact}", ca.ContactNo);
EmailMsg.SendMailMessage(ca.Name, "Applicant Person detail.", MessageString, mailto);
}).Start();
}
}
catch (Exception)
{
return Json("Error");
}
return Json("Success");
}