0

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

6
  • you can simple debug and see what actually data you get from the other side. I think that you get each data separate by comma. Commented Aug 3, 2016 at 23:02
  • @Aristos for debugging the first part is then what should be the parameter of web method so as to receive the serialized form then only the part of debugging arises Commented Aug 3, 2016 at 23:13
  • place on web method a code like System.Diagnostics.Debugger.Launch(); or Debug.Assert(false, "test"); and the debugger will start on that point. Commented Aug 3, 2016 at 23:20
  • Aristos still you dont understand my question public static int CreateUser( ParameterType ParameterName) Is the defenition right so what will be the parameterType for serialized data then only the process of checking with debugger makes sense right ? Commented Aug 3, 2016 at 23:26
  • Yes you have right, I did not understand it. The serialize must be one long string, so one string only parameter - but you must try and see, I am not sure how you serialize your data... Commented Aug 3, 2016 at 23:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.