Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
@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.

JavaScript
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,
C#
 [HttpPost]
        public ActionResult Advertisement(AdvertisementVM advertisementVM
{

}


the
advertisementVM.AdImageFileName
is passing null, not setting image name.

Please help me, how can I do this ?
Posted
Updated 22-Apr-17 23:09pm
v4

1 solution

JavaScript
$('#id').val(value-to-set); // just before your ajax call
 
Share this answer
 
Comments
Member 12955507 23-Apr-17 5:18am    
Yes, I am setting value '$('#hdAdImageFileName').val(res.AdvImage);' and it is happening after execute this ' this.SaveAdvtImages();' function. But only the 'advertisementVM.AdImageFileName' is passing null
Kornfeld Eliyahu Peter 23-Apr-17 5:22am    
Maybe your first ajax call never get into 'success'? Try to debug it...
Member 12955507 23-Apr-17 5:27am    
No, it is happening there. I did debugged there and getting Image name also. but after submit the form the hidden field value is binding. Actually I want to bind the value to hidden field before submit the form.
Kornfeld Eliyahu Peter 23-Apr-17 5:32am    
Move your assignment out of the ajax call, just before this.SaveAdvtImages();
Member 12955507 23-Apr-17 5:36am    
Excuse me! I can not understand, do you mean I have to move the '$('#hdAdImageFileName').val(res.AdvImage);' outside the 'this.SaveAdvtImages();' function ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900