I am having an issue with file uploads. When a user tries to create a new record in the table and one if the required fields is not filled out properly I get the "field is required" message and everything looks fine. However when I correct the required field error and try to create the record, it goes through without an error, but the file upload is not inserted into the database and the new record is not added to the table. Any thoughts?
```
//travel table
var traveleditor = new $.fn.dataTable.Editor({
ajax: '/api/DataTable/TravelTable',
table: '#TravelTable',
fields: [
{
"label": "FormInstanceId:",
"name": "FormInstanceId",
"className": "formidnew"
},
{
"label": "Date:",
"name": "date",
"type": "datetime",
"format": "MM/DD/YY"
},
{
"label": "Category:",
"name": "expensecategory",
"def": "Travel",
"className": "category"
},
{
"label": "Type of Expense:",
"name": "expense_type",
"type": "select",
"options": [
"Air Fare",
" Milege",
" Gas",
"Taxi",
"Other"
]
},
{
"label": "Description:",
"name": "description",
"type": "textarea"
},
{
"label": "Cost Center:",
"name": "cost_center",
"type": "select",
"placeholder": "Select a Cost Center"
},
{
"label": "Account Number:",
"name": "account_number",
"type": "select",
"placeholder": "Select an Account Number"
},
{
"label": "W\/O:",
"name": "wo_number"
},
{
"label": "Project:",
"name": "project_number",
"type": "select",
"placeholder": "Select a Project Number"
},
{
"label": "Amount:",
"name": "amount",
},
{
"label": "Attachment:",
"name": "attachments",
"type": "upload",
"display": function (Id) {
var ext = traveleditor.file('expenseAttachment', Id).webPath.toLowerCase();
var ext1 = ext.split(".").pop();
if (Id !== null) {
var ext = traveleditor.file('expenseAttachment', Id).webPath.toLowerCase();
var ext1 = ext.split(".").pop();
if (ext1 == "pdf") {
return '<a href ="' + traveleditor.file('expenseAttachment', Id).webPath + '" target=_blank><i class =" glyphicon glyphicon-file" style="font-size: 1em; "</i>' + traveleditor.file('expenseAttachment', Id).filename + '</center></a>';
}
if ((ext1 == "png") || (ext1 == "jpg") || (ext1 == "gif")) {
return '<a href ="' + traveleditor.file('expenseAttachment', Id).webPath + '" target=_blank><img width="100px" src="' + traveleditor.file('expenseAttachment', Id).webPath + '"/></a>';
}
}
else {
return "No File";
}
},
clearText: "Remove file",
fieldInfo: "Upload file, then create."
}
]
});
$('label', traveleditor.field('date').node()).addClass('aster');
$('label', traveleditor.field('description').node()).addClass('aster');
$('label', traveleditor.field('account_number').node()).addClass('aster');
$('label', traveleditor.field('amount').node()).addClass('aster');
$('label', traveleditor.field('cost_center').node()).addClass('aster');
traveleditor.on('open', function () {
var id1 = $("#thisid").val();
var id2 = $("#thisid2").val();
var cc = $("#CostC").val();
if (id1 != "") {
var id = id1;
}
else {
var id = id2;
}
$("#DTE_Field_FormInstanceId").val(id);
$("#DTE_Field_cost_center").val(cc);
$(".formidnew").hide();
$(".category").hide();
});
traveleditor.on('close', function () {
traveltable.ajax.reload();
});
//API Controller
public IHttpActionResult TravelTable()
{
var request = HttpContext.Current.Request;
using (var db = new Database("sqlserver", dbCon))
{
var response = new Editor(db, "ExpenseTable", "id")
.Where(q => q.Where("FormInstanceId", request.Form["Value"], "=").Where("expensecategory", "Travel", "="))
.Model<ExpenseTableModel>()
.Field(new Field("date")
.Validator(Validation
.DateFormat("MM/dd/yy"))
.GetFormatter(Format.DateSqlToFormat("MM/dd/yy"))
.SetFormatter(Format.DateFormatToSql("MM/dd/yy"))
.Validator(Validation.NotEmpty(
new ValidationOpts
{
Message = "The field is required"
}
)))
.Field(new Field("description")
.Validator(Validation.NotEmpty(
new ValidationOpts
{
Message = "The field is required"
}
))
)
.Field(new Field("account_number")
.Options(new Options()
.Table("ExpenseAccountNumber")
.Value("AccountNumber")
.Label("AccountNumber"))
.Validator(Validation.NotEmpty(
new ValidationOpts
{
Message = "The field is required"
}
)
))
.Field(new Field("amount")
.Validator(Validation.NotEmpty(
new ValidationOpts
{
Message = "The field is required"
}
)))
.Field(new Field("cost_center")
.Options(new Options()
.Table("CostCenters")
.Value("CostCenterNumber")
.Label("CostCenterNumber"))
.Validator(Validation.NotEmpty(
new ValidationOpts
{
Message = "The field is required"
}
)))
.Field(new Field("project_number")
.Options(new Options()
.Table("ExpenseProjectNumber")
.Value("ProjectNumber")
.Label("ProjectNumber"))
)
.Field(new Field("attachments")
.Upload(new Upload(request.PhysicalApplicationPath + @"ExpenseAttachments\Travel\2018\__NAME____ID____EXTN__")
.Validator(file => {
if (file.ContentLength >= 5000000)
{
return "Files must be smaller than 500K";
}
return null;
})
.Validator(
Validation.FileExtensions(
new[] { "png", "jpg", "gif", "pdf"},
"Only image or pdf files can be uploaded (png, jpg, gif and pdf)"
)
)
.Db("expenseAttachment", "Id", new Dictionary<string, object>
{
{ "filename", Upload.DbType.FileName },
{"url", Upload.DbType.SystemPath },
{"webPath", Upload.DbType.WebPath}
})
.DbClean(data =>
{
File.Delete("Id");
return true;
}
))
.SetFormatter(Format.NullEmpty()))
.Process(request)
.Data();
return Json(response);
}