0

I am trying to pass my form data along with image file from my form to controller method. I got reference from the links: https://qawithexperts.com/article/asp.net/file-uploading-using-jquery-ajax-in-mvc-single-or-multiple-f/98

How to Pass Image File and Form Data From Ajax to MVC Controller

What I did so far is:: My form:

<div class="col-md-4">
    <div class="form-group ">
        <label class="required title_header">Owner Name: </label>
        @Html.EditorFor(model => model.PropName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.PropName, "", new { @class = "text-danger" })
    </div>
</div>
// other form data
<div class="col-md-4">
    <div class="form-group ">
        <label class="required title_header">Image</label>
        <input type="file" class="form-control" id="ImageFile" required name="ImageFile" accept="image/*">
    </div>
</div>

<script>
$(document).ready(function () {
        $('#btn_submit').click(function () {
            var testModel = new FormData();         
            var PropName = $('#PropName').val();
            var PropAddress = $('#PropAddress').val();
            var PropContact = $('#PropContact').val();
            var Services = $('#Services').val();            
            var ImageFile = $('#ImageFile').get(0).files;
            var firmModel_ = {
                PropName: PropName,
                PropAddress: PropAddress,
                PropContact: PropContact,
                Services: Services
            }           
            testModel.append("firmModel_", JSON.stringify(firmModel_));
            testModel.append("ImageFile", ImageFile[0]);
            $.ajax({
                type: "POST",
                url: "/FirmModels/Index",
                data: testModel, //JSON.stringify(testModel),
                contentType: false, //"application/json;charset=utf-8",
                processData: false,
                dataType: 'json',
                success: function (response) {
                    if (response.success) {
                        console.log(response.responseText);
                    }
                },
                error: function (response) {
                    alert("failed...");
                }
            })
            return false;
        })
    })
</script>

And my controller code:

[HttpPost]
public JsonResult Index(FirmModel firmModel, HttpPostedFileBase ImageFile, string firmModel_)
{
    // code block
}

While debugging the controller code, the firmMOdel is null, the ImageFile parameter is gettting {System.Web.HttpPostedFileWrapper} and firmModel_ parameter is getting data as:

{"PropName":"raaj vaidaya","PropAddress":"HP","PropContact":"8195551590","Services":"lubricants suppliers"}

So I have some questions regarding the process,

  1. why data is not passing to the controller as an object, I mean why FirmModel firmModel is being NULL?
  2. The data I am getting in the string firmModel_ , how to use this data to save in database?
  3. Is there be any security issues when data is passing to the controller as string format?

Please help, thank you!!!

nischalinn
  • 1,053
  • 2
  • 10
  • 33
  • First of all you're not passing any value to the first parameter firmModel from the client side. If you want to use firmModel_ you can deserialize the json string to the specified object type using JsonConvert.DeserializeObject – Lirzae Nov 11 '21 at 09:56

0 Answers0