Passing Webmethod Parameter To Another Web Method
how can we call one web method in another web method using ajax from the first web method i need to call another web method and pass the parameters from the first to second method
Solution 1:
first Create 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(string name, string age)
{
string[] abc = { name, age };
return Json(abc);
}
}
}
then create Your view
@{
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();" />
Solution 2:
When you click, does it actually fires the click function? Did you debug it? If not, first try to put a breakpoint in the first line inside the click function or add a simple alert("click works"), maybe your selector is wrong.
Solution 3:
Also we can write the following solution in controller and fill the EmployeeDto in view side pass to 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);
}
}
}
Solution 4:
There can be a few corrections first of all you need document.ready event or another helper function (i changed to a helper function due to asp.net's clientid thing) and then you have to stop button posting back 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(r.d);
}
});
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);
}
Post a Comment for "Passing Webmethod Parameter To Another Web Method"