Member_2_1242703
asked on
Call Controller Action Method from ASPX
How do I call a method from a controller from a web form? I'm trying with this code:
Here is SubController.cs
What am I doing wrong?
<script type = "text/javascript">
$(document).ready(function () {
$('.btnClick').click(function () {
$.ajax(
{
type: "POST", //HTTP POST Method
url: "SubController/AddReq",
data: { //Passing data
Name: $("#txtEMPLID").val(),
City: $("#txtHours").val(),
Address: $("#txtComments").val()
}
});
});
});
</script>
Here is SubController.cs
public class SubController : Controller
{
private SqlConnection con;
// GET: AddEmployee
public ActionResult AddRequest()
{
return View();
}
//Post method to add details
[HttpPost]
public ActionResult AddReq(SubModel obj)
{
AddDetails(obj);
return View();
}
//To Handle connection related activities
private void connection()
{
string constr = ConfigurationManager.ConnectionStrings["cMain"].ConnectionString;
con = new SqlConnection(constr);
}
//To add Records into database
private void AddDetails(SubModel obj)
{
connection();
SqlCommand com = new SqlCommand("AddEmp", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EMPLID", obj.EMPLID);
com.Parameters.AddWithValue("@Hours", obj.Hours);
com.Parameters.AddWithValue("@Comments", obj.Comments);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER