大家好, 最近專案上遇到一個問題, 因為先前的開發者是混用Vue與Razor語法, 剛開始接觸Razor也不太熟悉用法, 要把v-model的值傳給@Html.TextBoxFor接vue-model的值, 不知道可不可行, 目前遇到的問題是用jquery抓var _project_Num = $("#project_Num").val()
的值, 但不知道有沒有辦法將_project_Num
的值再加到var frm = $("form#myPage")[0];var data = new FormData(frm);
傳遞的data裡面, 然後用ajax呼叫Controller, 去解析路徑與insert資料庫, 還是有其他方法可行 還請各位大大指教
檔案上傳功能
<form>
<div id="myPage">
<div class="mb-3 row">
<div class="col">
<label class="col-sm-3 col-form-label">project_id</label>
<input type="text"
id="project_Num"
class="form-control"
v-model="project.project_id" />
</div>
</div>
<div class="mb-3 row">
<div class="col">
<label>檔案名稱</label><font color="red">*</font>
@Html.TextBoxFor(x => x.FileUpload.file_name, new { @class = "form-control", placeholder = "請輸入檔案名稱" })
@Html.HiddenFor(x => x.FileUpload.id)
</div>
</div>
<div class="mb-3 row">
<div class="col">
<label>檔案路徑</label><font color="red">*</font>
@Html.TextBoxFor(x => x.FileUpload.file_upload, new { type = "file", @class = "form-control", placeholder = "請選擇檔案" })
</div>
</div>
<button type="button" id="fileUpload" class="btn btn-success">送出</button>
</div>
</form>
Jquery
$('#fileUpload').click(function () {
// 取project_Num的值
var _project_Num = $("#project_Num").val()
// 有辦法把_project_Num值加到data裡面嗎?
var frm = $("form#myPage")[0];
var data = new FormData(frm);
$.ajax({
type: "POST",
url: '/Project/FileUpload',
dataType: "json",
cache: false,
enctype: 'multipart/form-data',
data: data,
processData: false,
contentType: false,
success: function (resultdata) {},
error: function () {}
});
return false;
});
Controller
[HttpPost]
public ActionResult FileUpload(FileUploadModel md)
{
FileUploadBI bi = new FileUploadBI();
Dictionary<string, string> dic = new Dictionary<string, string>();
string sFilePath = System.Web.Configuration.WebConfigurationManager.AppSettings["FilePath"].ToString();
// SaveUpload 判斷檔名與儲存路徑
string sFName = bi.SaveUpload(md.FileUpload.file_upload, sFilePath, "|.pdf|.jpg|.png|.doc|.docx|.xls|.xlsx|");
// InsertFileUpload insert Database
if (bi.InsertFileUpload(md.FileUpload)
{
dic.Add("code", ...);
dic.Add("message", ..);
dic.Add("messagetype",..);
}
else
{
dic.Add("code", ...);
dic.Add("message", ...);
dic.Add("messagetype", ...);
}
return Json(dic);
}