如何在提交表单之前将图像名称绑定到hiddenfield? [英] How to bind image name to hiddenfield before submit form ?
本文介绍了如何在提交表单之前将图像名称绑定到hiddenfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MVC5中工作,在项目文件夹上保存上传图像后,我想使用隐藏字段值将图像名称绑定到模型对象,
查看,
@ Html.HiddenFor(m => m.AdImageFileName,new {@id =hdAdImageFileName})
我的尝试:
通过使用jQuery下面的代码,我保存图像并返回并将图像名称绑定到隐藏字段作为值。
Advertisement = {
Addform: function (e){
this 。 SaveAdvtImages();
$ .ajax({
url: / UserRegister / Advertisement,
类型: POST,
data:$(' form')。serializeArray(),
success: function (结果){
$(' .loading')。hide( );
$( .modal-content)。height( 225 );
$(' #alertMsgModalContent')。html(result);
$(' #alertMsgModal'跨度>)模态(选项);
$(' #alertMsgModal')。modal(' show');
},
错误:功能(结果){
}
});
},
SaveAdvtImages: function (){
var formData = new FormData($(' # frmAdvertisement')[ 0 ]);
$(' .loading')。show();
$ .ajax({
url:' ../ UserRegister / SaveAdvImages /',
类型:' POST',
data:formData,< span class =code-comment> // {$ formData,pageVM:hidJson},
contentType: false ,
processData: false ,
成功: function (res){
$(' #hdAdImageFileName').val(res.AdvImage);
},
错误: function (数据){
}
});
}
}
但在提交'
$之前( 'form')。serializeArray()
'我必须设置
m => m.AdImageFileName
模型对象。现在,
[HttpPost]
public ActionResult Advertisement(AdvertisementVM advertisementVM
{
}
advertisementVM.AdImageFileName
是传递null,不设置图像名称。
请帮帮我,我该怎么做?
解决方案
.ajax({
url: / UserRegister / Advertisement,
类型: POST,
data:
(' form')。serializeArray(),
成功: function (result){
(' .loading'< /跨度>)隐藏();
I am working in MVC5, After save the upload image on project folder then I want to bind the image name to model object using hidden field value,
View,
@Html.HiddenFor(m => m.AdImageFileName, new { @id = "hdAdImageFileName" })
What I have tried:
By using jQuery below code, I have saving image and return and bind the image name in to the Hidden field as value.
Advertisement = {
Addform: function (e) {
this.SaveAdvtImages();
$.ajax({
url: "/UserRegister/Advertisement",
type: "POST",
data: $('form').serializeArray(),
success: function (result) {
$('.loading').hide();
$(".modal-content").height(225);
$('#alertMsgModalContent').html(result);
$('#alertMsgModal').modal(options);
$('#alertMsgModal').modal('show');
},
error: function (result) {
}
});
},
SaveAdvtImages: function () {
var formData = new FormData($('#frmAdvertisement')[0]);
$('.loading').show();
$.ajax({
url: '../UserRegister/SaveAdvImages/',
type: 'POST',
data: formData, //{ $formData, pageVM: hidJson },
contentType: false,
processData: false,
success: function (res) {
$('#hdAdImageFileName').val(res.AdvImage);
},
error: function (data) {
}
});
}
}
But before submit the '
$('form').serializeArray()
' I have to set
m => m.AdImageFileName
model object. Now,
[HttpPost]
public ActionResult Advertisement(AdvertisementVM advertisementVM
{
}
the
advertisementVM.AdImageFileName
is passing null, not setting image name.
Please help me, how can I do this ?
解决方案
.ajax({ url: "/UserRegister/Advertisement", type: "POST", data:
('form').serializeArray(), success: function (result) {
('.loading').hide();
这篇关于如何在提交表单之前将图像名称绑定到hiddenfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文