Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ajax url not calling controller...

url :'Home/AddContact'
but i saw that in console ..it get Home/Home/AddContact ...i dont no why ..below is my code...

What I have tried:

//controller
public ActionResult AddContact(MContact mc,int id)
{
try
{
var data = bal.addcontact(mc, id);
Result.Success = true;
Result.Data = data;

}
catch (Exception)
{
Result.Success = false;
Result.Message = "Data Not Insert";
}
return Json(Result,JsonRequestBehavior.AllowGet);
}


//js

var submitcontact = function () {
// $("#SupplierModal").find(".has-error").removeClass("has-error");
var id = $("#contact-id").val();
var isvalid = true;

if ($("#contact-name").val().trim() == "") {
isvalid = false;
$("#contact-name").parent().parent().addClass("has-error");
}

if ($("#contact-email").val().trim() == "") {
isvalid = false;
$("#contact-email").parent().parent().addClass("has-error");
}

if ($("#contact-Subject").val().trim() == "") {
isvalid = false;
$("#contact-Subject").parent().parent().addClass("has-error");
}

if ($("#contact-Message").val().trim() == "") {
isvalid = false;
$("#contact-Message").parent().parent().addClass("has-error");
}
if (isvalid) {
savecontact(id, $("#contact-name").val().trim(), $("#contact-email").val().trim(), $("#contact-Subject").val().trim(), $("#contact-Message").val().trim());
}

};

var savecontact = function (id, name, email, subject, message) {

$.ajax({

url:"Home/AddContact",
type: 'POST',
data: {
id: id,
name: name,
email: email,
subject: subject,
message: message,
},
success: function (result) {

if (result.success) {
alert();

$("#contact-name").val("");
$("#contact-email").val("");
$("#contact-Subject").val("");
$("#contact-Message").val("");
$("#contact-id").val(0);

// getSupplierList();
}
else {
//error
}
},
error: function (result) {
console.log("In Error");
}
});
}
Posted
Updated 4-Oct-16 1:44am
Comments
Sreekanth Mothukuru 4-Sep-16 11:48am    
Are you using "Areas"? check your route once..
Afzaal Ahmad Zeeshan 4-Sep-16 14:53pm    
Can you use url: "~/Home/AddContact"?

HI,

Try to hit the controller path in webbrowser and test.

Also your can try for "~/Home/AddContact" or "../Home/AddContact" .
 
Share this answer
 
You should use @Url.Action

JavaScript
url: '@Url.Action("AddContact", "Home")',


assuming the js is on the view directly.
 
Share this answer
 
please use [httppost] attibute on that action.


you are passing different data like this

JavaScript
data: {
id: id,
name: name,
email: email,
subject: subject,
message: message,
},


so it is basically looking for method which have these paramenter..
and ur action has only contact object as parameter.

better
var data0 = {
name: $("#contact-name").val(),
email:$("#contact-email").val(),
subject:$("#contact-Subject").val(),
contact:$("#contact-Message").val(),
id:$("#contact-id").val()};

var jsonContact = JSON2.stringify(data0);



JavaScript
$.ajax({

url:"Home/AddContact",
type: 'POST',
data: {MContact :jsonContact},
success: function (result) {

if (result.success) {
alert();

$("#contact-name").val("");
$("#contact-email").val("");
$("#contact-Subject").val("");
$("#contact-Message").val("");
$("#contact-id").val(0);

// getSupplierList();
}
else {
//error
}
},
error: function (result) {
console.log("In Error");
}
})



and your action should be like


C#
//controller
public ActionResult AddContact(MContact mc)
{
try
{
var data = bal.addcontact(mc, id);
Result.Success = true;
Result.Data = data;

}
catch (Exception)
{
Result.Success = false;
Result.Message = "Data Not Insert";
}
return Json(Result,JsonRequestBehavior.AllowGet);
}
 
Share this answer
 

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



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