After much of struggle im posing this question. Im using a Kendo Upload on a page. Am able to post the selected files on the asyn mode with whe the page has Html.BeginForm. But I'm not able to send file details as HttpPostedFileBase when I use ajax request to send data to the controller.
Following is my html
<form class="form-horizontal" role="form">
<div class="form-group">
@Html.Label("Complaint Name", new { @class = "col-sm-3 control-label" })
<div class="col-sm-4">
@Html.TextBoxFor(
m => m.ComplaintName,
new
{
@TabIndex = "1",
@class = "form-control input-sm",
disabled = true
})
</div>
</div>
<div class="form-group">
@Html.Label("Complaint Details", new { @class = "col-sm-3 control-label" })
<div class="col-sm-4">
@Html.TextBoxFor(
m => m.ComplaintDetails,
new
{
@TabIndex = "2",
@class = "form-control input-sm",
disabled = true
})
</div>
</div>
<div class="form-group">
@Html.Label("Choose files to upload", new { @class = "col-sm-3 control-label" })
<div class="col-sm-9 nopaddingforTextArea">
<input name="files" id="files" type="file" />
</div>
</div>
<div class="form-group">
<div>
<input id="btnSubmit" class="btn btn-primary pull-right" type="button" />
</div>
</div>
</form>
Following is my action
public ActionResult SaveComplaintDetails(string complaintName, string complaintDetails, IEnumerable<HttpPostedFileBase> files)
{
}
Following is my js
$("#files").kendoUpload({
async: {
saveUrl: '@Url.Action("EsuperfundCommentsBind", ClientInboxConstants.NewQuery)',
autoUpload: false
},
multiple: true
});
$("#btnSubmit").click(function () {
//Ajax call from the server side
$.ajax({
//The Url action is for the Method FilterTable and the Controller PensionApplicationList
url: '@Url.Action("SaveComplaintDetails", "Complaints")',
//The text from the drop down and the corresponding flag are passed.
//Flag represents the Index of the value in the dropdown
data: {
complaintName: document.getElementById("ComplaintName").value,
complaintDetails: document.getElementById("ComplaintDetails").value,
files: //What to pass here??
},
contentType: "application/json; charset=utf-8",
//Json data
datatype: 'json',
//Specify if the method is GET or POST
type: 'GET',
//Error function gets called if there is an error in the ajax request
error: function () {
},
//Gets called on success of the Ajax Call
success: function (data) {
}
});
});
My question is how to pass the selected files in Kendo Upload in ajax as a parameter? Any help in this aspect would be really appreciated.
after pressing the button, the controller null comes to how to fix. 2 days already sitting, and the time comes to an end
If your view is based on a model and you have generated the controls inside
<form>
tags, then you can serialize the model to FormData using:This will also include any files generated with:
<input type="file" name="myImage" .../>
and post it back using:and in your controller:
or (if your model does not include a property for HttpPostedFileBase)
If you want to add additional information that is not in the form, then you can append it using
Example Usage :
View :
Controller :
Hope this helps...