0

I have a json collection and I want to send one textbox value to controller through jQuery ajax

 $('#btnsave').click(function (e) {
    debugger;
    $.ajax({
        type: "POST",
        url: '/Asset/SaveAssociate',
        data: "{'data':'"+JSON.stringify(allVals)+"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(data);
         //   var gridk = $("#grid1").data("kendoGrid");
          //  gridk.dataSource.read();
        }
    })
})

and this is my textbox value

 var _assetid = $("#AssetId").val().trim();

like data: { json and assetid :assetid } can I pass like this

This is my action method

[NoCache]
public ActionResult SaveAssociate(string data, string AssetId)
{
            JavaScriptSerializer json = new JavaScriptSerializer();
            List<GetUserdata> myObjs = new List<GetUserdata>();
            myObjs = json.Deserialize<List<GetUserdata>>(data);

            for (int i = 0; i <= myObjs.Count; i++)
            {
            }

            return Content("Hai");
}
1
  • I want to send Json collection and textbox value parameter to action method .collection is coming but if i go like this data:{json and assetid :assetid } assetid is not coming at action method Commented May 17, 2014 at 13:29

1 Answer 1

1

I believed, this is what you want

 $('#btnsave').click(function (e) {
        debugger;

       var dataToSend={
          'data':JSON.stringify(allVals),
          'AssetId':$("#AssetId").val().trim()
        };
        $.ajax({
            type: "POST",
            url: '/Asset/SaveAssociate',
            data: JSON.stringify(dataToSend),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data);
             //   var gridk = $("#grid1").data("kendoGrid");
              //  gridk.dataSource.read();
            }
        })
    })
Sign up to request clarification or add additional context in comments.

6 Comments

At this point 'AssetId':$("#AssetId").val().trim() am getting value but when it comes to dataToSend.AssetId it is coming null
Can you see the AssetId value being passed in request?
Also, please show your code for Action that is suppose to accept these parameters
I guess, you have to put dataToSend inside click event, check out the updated code.
Yes but ajax is not calling Action method if i kept like this
|

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.