How to upload Iform File in iframe was binding null javascript jquery

0 votes

In asp.net core , i define ProductUpdateRequest has  public IFormFile ThumbnailImage { get; set; } . I create update form in iframe. when I submit form with

jQuery(function ($) {
    $("#btnsubmit").click(function (e) {
        
        //Serialize the form datas.   
        var valdata = $("#updateProductForm").serialize();
        
        //to get alert popup   
        $.ajax({
            url: "/Products/Update",
            data: valdata,
            dataType: "json",
            type: "POST",
            success: function (response) {
                alert(1);
            }
        });
    });   
    
});

asp.net have bind data from the form to request model :

[HttpPost]
        public async Task<IActionResult> Update([FromForm] ProductUpdateRequest request)

but request.ThumbnailImage is null. How can I post img (Iform File) to the server by ajax from iframe? When update action is done, what would I do if I want to get a response to

success: function (response) {
                alert(1);
            }

and told to parent page to reload to update new data? Help me, I can't understand this problem...

Aug 19, 2022 in Web Development by gaurav
• 23,260 points
1,705 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I work with asp mvc, not core.

public IFormFile ThumbnailImage {get; set; } change to :

public HttpPostedFileBase ThumbnailImage {get; set; } 

in your view change form to this code:

@using (Html.BeginForm("Update", "Home", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "return SubmitForm(this)" }))
{
   
    <div class="form-group">
        <input name="ThumbnailImage" type="file" />
    </div>

    <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-primary" />
    </div>
}

and script change to this code:

function SubmitForm(form) {
      var formData = new FormData($(form)[0]);
        $.ajax({
          url: form.action,
          type: form.method,
          data: formData,
          processData: false,
          contentType: false,

          success: function (response) {
             alert(response);
          }
     });
     return false;
}

and in controller change your action to this:

[HttpPost]
public async Task<JsonResult> Update(ProductUpdateRequest model)
{
    //update db
    return Json("success");
}
answered Aug 22, 2022 by rajatha
• 7,680 points

edited Mar 5

Related Questions In Web Development

0 votes
0 answers

How to filter JSON Data in JavaScript or jQuery?

How to filter JSON data using Javascript ...READ MORE

Jul 1, 2022 in Web Development by rajatha
• 7,680 points
2,579 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 14, 2024 in Web Development by anonymous
• 12,580 points
151 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 21, 2024 in Web Development by Nidhi
• 12,580 points
285 views
0 votes
1 answer

How to download a file by jQuery.Ajax?

Hello @kartik, You don't need to do this ...READ MORE

answered Sep 18, 2020 in Web Development by Niroj
• 82,840 points
7,621 views
0 votes
1 answer

how to use substr() function in jquery?

To get substring of a string in ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,371 views
0 votes
1 answer

How to loop through array in jQuery?

Generic loop: var i; for (i = 0; i ...READ MORE

answered Jun 28, 2022 in Web Development by rajatha
• 7,680 points
2,361 views
0 votes
1 answer

Ajax using JQuery in ASP .NET c#

jQuery Ajax in ASP.Net $.ajax({      & ...READ MORE

answered Jun 15, 2022 in JQuery by rajatha
• 7,680 points
4,378 views
0 votes
0 answers

UL subdrop not working with jquery in Asp.Net C#

Keep searching why sub drop not working ...READ MORE

Jul 29, 2022 in Web Development by gaurav
• 23,260 points
455 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
1,101 views
0 votes
1 answer

Embedding Power Bi Report Promise is not defined powerbi.js

IE does not yet support Promise. You ...READ MORE

answered Oct 30, 2018 in Power BI by Shubham
• 13,490 points
2,192 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP