我想在路径中存储文件名,但是我的模型图像文件类型是一个字符串,这就是为什么无法定义错误的原因。当我使用公共httppostfilebase时,仅当我使用字符串然后显示文件名时,文件名才不会显示在控制器中,所以当我使用字符串时,请遵循以下代码行,我要存储文件名。任何专家都告诉我如何解决此问题。
错误:字符串数据类型有问题。
C#
[HttpPost]
public ActionResult mQuotationInsert(int Qt_ID, string EnteryDate, string Purpose, Quotation[] Quot, string AddNew)
{
string result = "Error! Order Is Not Complete!";
try
{
objQuotation.QuotationInsert(Qt_ID, EnteryDate, Purpose, Quot, AddNew);
ModelState.Clear();
result = "Quotation Inserted Successfully!";
return Json(result, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
throw;
}
}
public int QuotationInsert(int Qt_ID, string EnteryDate, string Purpose, Quotation[] Quot, string AddNew)
{
try
{
con.Open();
tr = con.BeginTransaction();
if (AddNew == "New")
{
cmd = new SqlCommand("Select Right('00' + Cast(ISNULL(MAX(Qt_ID),0)+1 as varchar(2)) + '', 2) from QuotationMain", con);
cmd.Transaction = tr;
Qt_ID = Convert.ToInt32(cmd.ExecuteScalar().ToString());
cmd = new SqlCommand("Sp_QuotationMainInsert", con);
}
else
cmd = new SqlCommand("Sp_QuotationMainUpdate", con);
cmd.Parameters.AddWithValue("@Qt_ID", Qt_ID);
cmd.Parameters.AddWithValue("@Comp_ID", 1);
if (EnteryDate != null)
cmd.Parameters.AddWithValue("@EnteryDate", EnteryDate);
else
cmd.Parameters.AddWithValue("@EnteryDate", string.Empty);
cmd.Parameters.AddWithValue("@Username", HttpContext.Current.Session["AgentName"]);
cmd.Parameters.AddWithValue("@Purpose", Purpose);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
if(Quot !=null)
{
for (int i = 0; i < Quot.Length; i++)
{
try
{
string fileName = Path.GetFileNameWithoutExtension(Quot[i].imageFile.FileName);
string extension = Path.GetExtension(Quot[i].imageFile.FileName);
fileName = fileName + DateTime.Now.ToString("dd/MM/yyyy") + extension;
Quot[i].imagePath = "~/AppFiles/Images/" + fileName;
fileName = Path.Combine(HttpContext.Current.Server.MapPath("~/AppFiles/Images/"), fileName);
Quot[i].imageFile.SaveAs(fileName);
if (AddNew == "New")
{
cmd = new SqlCommand("Select ISNULL(MAX(Qt_Dt_ID), 0) + 1 from QuotationDetail", con);
cmd.Transaction = tr;
mQt_Det_ID = Convert.ToInt32(cmd.ExecuteScalar());
cmd = new SqlCommand("Sp_QuotationDetailInsert", con);
cmd.Parameters.AddWithValue("@Qt_Dt_ID", mQt_Det_ID);
}
else if (AddNew == "Edit")
{
cmd = new SqlCommand("Sp_QuotationDetailUpdate", con);
cmd.Parameters.AddWithValue("@Qt_Dt_ID", Quot[i].Qt_Dt_ID);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Qt_Dt_ID", mQt_Det_ID);
cmd.Parameters.AddWithValue("@Qt_ID", Qt_ID);
cmd.Parameters.AddWithValue("@SrNo", Quot[i].Srno);
cmd.Parameters.AddWithValue("@PartyName", Quot[i].PartyName);
cmd.Parameters.AddWithValue("@IsMature", Quot[i].IsMature);
if (Quot[i].imagePath != null)
cmd.Parameters.AddWithValue("@Image", fileName);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
}
}
tr.Commit();
return i;
}
catch (SqlException sqlex)
{
tr.Rollback();
throw sqlex; // read all sql error
}
catch (Exception ex)
{
tr.Rollback();
throw ex; // General execption
}
finally
{
con.Close();
}
}
}
型号
[NotMapped]
public string imageFile { get; set; }
javascript
function saveQuotation(data) {
if ($.trim($("#txtQtID").val()) == "" || $.trim($("#txtNEnteryDate").val()) == "" || $.trim($("#txtNPurpose").val()) == "") return;
return $.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: "/Home/mQuotationInsert",
data: data,
success: function (result) {
alert(result);
location.reload();
},
error: function () {
alert("Error!")
}
});
}
$("#saveQuotation").click(function (e) {
e.preventDefault();
var QuotationArr = [];
QuotationArr.length = 0;
$.each($("#detailsTable tbody tr"), function () {
QuotationArr.push({
Srno: $(this).find('td:eq(0)').html(),
PartyName: $(this).find('td:eq(1)').html(),
imageFile=$(this).find('td:eq(2)').html(),
IsMature: $(this).find('td:eq(3)').html()
});
});
var data = JSON.stringify({
Qt_ID: parseInt($("#txtQtID").val()),
EnteryDate: $("#txtNEnteryDate").val(),
Purpose: $("#txtNPurpose").val(),
Quot: QuotationArr,
AddNew: $("#AddNew").val()
});
$.when(saveQuotation(data)).then(function (response) {
console.log(response);
}).fail(function (err) {
console.log(err);
});
});