0

I want to pass the data from view to controller with an uploaded file. My model name is SnapShotReceivedData. This is the way I did it. but not work.

In the form:

     <div class="col-md-6">
        <div class="form-group row">
            <label class="col-md-5 col-form-label requiredexempt">Reason</label>
            <div class="col-md-7">
                <select class="form-control" name="reason" id="reasonexe">
                    <option value="">Choose...</option>
                    <option value="Death ORDER">Death</option>
                    <option value="Other ORDER">Other</option>
                </select>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-md-5 col-form-label requiredexempt">Comment</label>
            <div class="col-md-7">
                <textarea class="form-control" rows="3" style="width:100%;" id="txtCommentexe"></textarea>

            </div>
        </div>
        <div class="form-group row">
            <label class="col-md-5 col-form-label">attachment</label>
            <div class="col-md-7">
                <input type="file" class="form-control" id="file" name="file"><br><br>
            </div>
        </div>

    </div>

submit button click JavaScript function:

function submit()
{
            var REASON_EXEMPT= $('#reasonexe').val();
            var COMMENT_EXEMPT= txtCommentexe.value;
            var $file = document.getElementById('file'),
                $formData = new FormData();

            if ($file.files.length > 0) {
                for (var i = 0; i < $file.files.length; i++) {
                    $formData.append('file-' + i, $file.files[i]);
                }
            }
           var SnapShotReceivedData = [{               

                REASON: REASON_EXEMPT,
                COMMENT: COMMENT_EXEMPT,
                ATTACHMENT: $formData
            }];
             SnapShotReceivedData = JSON.stringify({ 'SnapShotReceivedData': SnapShotReceivedData});

            $.ajax({
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                type: 'POST',
                url: '@Url.Action("SetHCMNIDSnapData", "Home")',
                data:  SnapShotReceivedData,
                success: function (result) {                   
                 
                    });
                    if (result=='10') {
                    }
                    if (result == '12') {
                    }
                },
                failure: function (response) {
                    //  $('#result').html(response);
                }
            });


}

model:

public class SnapShotReceivedData
{
        public string REASON { get; set; }
        public string COMMENT { get; set; }
        public string ATTACHMENT { get; set; }
}

controller:

        public JsonResult SetHCMNIDSnapData(List<SnapShotReceivedData> sObject)
        {
            if (Request.Files.Count > 0)
            {
                string a = "";
            }
         }

Always ATTACHMENT inside the sObject is null. please help me to solve this.

hotflix
  • 41
  • 4
  • There's a few issues here. First, to upload a file in AJAX you need to send a FormData object in the AJAX request and set `contentType` and `processData` in the `$.ajax()` settings to `false`. Second, you need to accept the file as the correct type in your C# code - a `string` isn't going to work here. More details: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 – Rory McCrossan Jun 23 '22 at 09:54
  • If you can submit the answer with corrections, It will be more helpful to me. @RoryMcCrossan – hotflix Jun 23 '22 at 10:01

0 Answers0