Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
hai all,while i m calling ajax calls i not able to call methos in my controller here is my code



TIA

What I have tried:

<title>ViewCompanyProfile</title>
@{
UsersDAL obj = new UsersDAL();
List<SelectListItem> CompanyName = new List<SelectListItem>();
DataTable dtCompanyDetails = obj.ShowCompany();
foreach (DataRow row in dtCompanyDetails.Rows)
{
CompanyName.Add(new SelectListItem { Value = row["CompanyId"].ToString(), Text = row["CompanyName"].ToString() });
}
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script>
function ShowCompany($this)
{
var CompanyName = $("#Company_Name").val();
alert("hello");
$.ajax({
URL: '/Admin/ShowCompany',
type:"GET",
datatype:"JSON",
data: { CompanyId: CompanyName },
success:function(s){
document.getElementById("table").innerHTML='

'+s[0]+'


'+s[1]+'

'+s[2]+'

'+s[3]+'

';
alert("hello");
}
});
alert("Welcome");
}
</script>
</head>
<body>

@using (Html.BeginForm())
{
@Html.ValidationSummary(true)

Select Company


@Html.DropDownListFor(Model => Model.CompanyName,CompanyName ,"Select", new {@id="Company_Name",@onchange="ShowCompany(this)" })
}

</body>


and in my controller

public ActionResult ViewCompanyProfile()
{
return View();
}
[HttpPost]
public ActionResult ShowCompany(int CompanyId)---------This one is not calling
{
DataTable dt = _objDAL.GetShowCompany(CompanyId);
int rowcount = dt.Columns.Count;
string[] strarray=new string[rowcount];
for (int i = 0; i < rowcount; i++)
{
strarray[i] = dt.Rows[0][i].ToString();
}
return Json(strarray,JsonRequestBehavior.AllowGet);
}
}
Posted
Updated 29-May-16 23:57pm
Comments
Prateek Dalbehera 30-May-16 3:39am    
Hi,
My initial diagnosis shows that you used "GET" instead of "POST" in ajax call.
I think it wont hit the server at all as its GET.
Please change the following and try again,

type:"GET", should be type:"POST", as in controller its post.
Member 12519727 30-May-16 4:00am    
i already tried this one sir.still i m not able to call my action method..it says failed to load resources something..
Member 11902764 30-May-16 5:46am    
just try with 'dataType' instead of 'datatype', also use contentType as below.

dataType: 'json',
contentType: "application/json; charset=utf-8",
Karthik_Mahalingam 30-May-16 5:56am    
var CompanyName = $("#Company_Name").val();
alert(CompanyName )
check you are getting the value
Member 12519727 1-Jun-16 3:25am    
the alert says undefined sir..i dont understand whats wrong with one i m not able to understand why this happening

1 solution

JavaScript
$.ajax({
            type: "GET",
            url: "/Admin/ShowCompany",
            data: "{}",
            contentType: "application/json; charset=utf-8", //try using contentType also
            dataType: "json", //instead of 'datatype'
            success: function (r, textStatus, jqXHR) {
                //If success do stuff here...
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(textStatus + "_" + errorThrown);
            }
        });
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900