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.