Passing the webmethod parameter to another web method

as we can name one web method in another web method, using ajax from the first web method, I need to call another web method and pass parameters from the first to the second method.

<script type = "text/javascript"> function GetCityNameArray() { var cities = ['Mumbai', 'Delhi', 'Chennai']; PageMethods.GetCityNameArray(cities, OnSuccessGetCityNameArray); } function OnSuccessGetCityNameArray(response) { for (var i in response) { alert(response[i]); } } </script> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true"> </asp:ScriptManager> <div> <input type = "button" onclick = "GetCityNameArray()" value = "Get City Name Array" /> </div> </form> protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string[] GetCityNameArray(string[] cities) { return cities; } 
0
source share
4 answers

create controller first

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace DemoApp.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public JsonResult Check(string name, string age) { string[] abc = { name, age }; return Json(abc); } } } 

then create your look

  @{ ViewBag.Title = "Index"; } <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"> </script> <script type="text/javascript"> function GetData() { var name = txtName.value; var age = txtAge.value; jQuery.ajax({ type: "Post", url: '@Url.Action("Check", "Home")', dataType: "json", contentType: "application/json; charset=utf-8", data: JSON.stringify({ name: name, age: age }), success: function (data) { alert("Name=" + data[0] + " And " + "Age=" + data[1]); } }); return false; } </script> Name :<input id="txtName" type="text" /> Age : <input id="txtAge" type="text" /> <input id="btnSubmit" type="button" value="button" onclick="return GetData();" /> 
0
source

When you click, does this click function really work? Did you debug it? If not, first try putting a breakpoint in the first line inside the click function or add a simple warning ("click works"), your selector may be wrong.

0
source

In addition, we can write the following solution in the controller and fill in the EmployeeDto in view field on the controller.

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace DemoApp.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(); } public JsonResult Check(EmployeeDto EmployeeDto) { string[] abc = { EmployeeDto.name, EmployeeDto.age }; return Json(abc); } } } 
0
source

There may be several fixes, first of all you need the document.ready event or another helper function (I switched to the helper function due to the asp.net clientid function), and then you need to stop the submit button of your page.

 script type="text/javascript"> var onSubmitClick = function() { var button = this; var obj = {}; obj.name = $.trim($("[id*=txtName]").val()); obj.age = $.trim($("[id*=txtAge]").val()); $.ajax({ type: "POST", url: "CS.aspx/SendParameters", data: JSON.stringify(obj), contentType: "application/json; charset=utf-8", dataType: "json", success: function (r) { alert(rd); } }); return false; }; </script> </head> <body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> Name: </td> <td> <asp:TextBox ID="txtName" runat="server" Text = "" /> </td> </tr> <tr> <td> Age: </td> <td> <asp:TextBox ID="txtAge" runat="server" Text = ""/> </td> </tr> <tr> <td> <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="return onSubmitClick();" UseSubmitBehaviour="false" /> </td> </tr> </table> protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string SendParameters(string name, int age) { return string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine); } 
-1
source

All Articles