Html
<div class="form-group">
<label for="firstName">First Name:</label>
<input type="text" class="form-control" id="firstName" name="firstName" runat="server"/>
</div>
<div class="form-group">
<label for="lastName">Last Name:</label>
<input type="text" class="form-control" id="lastName" name="lastName" runat="server"/>
</div>
<div class="form-group">
<input type="file" Class="form-control" id="fileUpload" name="fileUpload" runat="server" />
</div>
<input type="button" id="submit" class="btn btn-default" runat="server" value="Submit"/>
</form>
Javascript
$(document).ready(function(){
$("#submit").on("click", function (e) {
// alert($("form").serialize());
var obj = '{"firstName":' +'"'+$("#firstName").val() + '"'+', "lastName":' + '"'+ $('#lastName').val() + '"}';
console.log(obj);
$.ajax({
type: "POST", url: "WebForm1.aspx/CreateUser", data: obj /*$("form").serialize()*/
,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
console.log("Call Success");
alert(data.d);
},
failure: function (data) {
console.log("Call Fail");
alert(data.d);
},
error: function (data) {
console.log("Call Error");
alert(data.d);
}
});
});
});
Web Method
[System.Web.Services.WebMethod]
public static int CreateUser(String firstName, String lastName)
{
Users userObj = new Users();
userObj.fileName = "xyz";// user.fileName;//fileUpload.PostedFile.FileName;
userObj.firstName = firstName;//firstName.Value;
userObj.lastName = lastName;//lastName.Value;
// fileUpload.SaveAs(Server.MapPath("~/") + "/Uploads/" + fileUpload.PostedFile.FileName);
// System.Diagnostics.Debug.WriteLine( Server.MapPath("~/") + "/Uploads/" + fileUpload.PostedFile.FileName);
SQLDAL sqldalobj = new SQLDAL();
int success = sqldalobj.Create(userObj);
// System.Diagnostics.Debug.Write(user);
return success;
}
Right now I am able to pass text box values as json data and inside web method its received correctly.But when I need a larger number of text boxes using current approach its would result in large number of parameters which I want to avoid using a object as parameter instead of different parameters for each html form element.And using this approach I am failing upload the file .So my question is if I serialize the form and send the data to webmethod how to properly recieve the said serialized form and retrieve the data from it including the fileupload
System.Diagnostics.Debugger.Launch();orDebug.Assert(false, "test");and the debugger will start on that point.