Reputation:
I want to send my ajax data through post method, from my view but I don't how to receive it in the controller method. here is ajax code :
$.ajax({
contentType: 'application/json; charset=utf-8',
method: 'post',
async: false,
url: "Gateway/DB_Rola",
data:Obj,
success: function (Data) {alert("Good");},
error: function () {alert("Error");}
});
Here is controller code :
[HttpPost] // bindig data with class thing
public JsonResult DB_Rola(thing things)
{ ... }
modal class code :
public class thing
{
public int surah { get; set; }
public int ayah { get; set; }
public string verse { get; set; }
}
Now I don't know how to get that object in controller method. I have tried it with get
method , there its working fine, problem is with post
...
Upvotes: 1
Views: 1257
Reputation: 11154
Please try with the below code snippet.
JS
<script>
$(document).ready(function () {
var things = new Object();
things.surah = 1;
things.ayah = 2;
things.verse = "3";
$.ajax({
url: "Home/DB_Rola?count=" + 1,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
data: JSON.stringify(things),
success: function (result) {
alert('success');
},
error: function (result) {
alert('boo!');
},
});
});
</script>
MODEL
public class thing
{
public int surah { get; set; }
public int ayah { get; set; }
public string verse { get; set; }
}
CONTROLLER
public class HomeController : Controller
{
[HttpPost]
public ActionResult DB_Rola(thing things)
{
return Json(new { IsSuccess = true });
}
}
OUTPUT:
With/Without Count
Update 1 : pass the list[] of objects instead of single object things
<script>
$(document).ready(function () {
var things = new Array();
var test1 = new Object();
test1.surah = 1;
test1.ayah = 2;
test1.verse = "3";
things.push(test1);
var test2 = new Object();
test2.surah = 11;
test2.ayah = 22;
test2.verse = "33";
things.push(test2);
$.ajax({
url: "Home/DB_Rola?count=" + 1,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
data: JSON.stringify(things),
success: function (result) {
alert('success');
},
error: function (result) {
alert('boo!');
},
});
});
</script>
[HttpPost]
public ActionResult DB_Rola(List<thing> things,int count)
{
return Json(new { IsSuccess = true });
}
Upvotes: 1
Reputation: 4146
assuming Obj = {surah:somvalue, ayah:somevalue, verse:somevalue}
The problem is the extra parameter count, you don't have a post action that handles count,
so try:
$.ajax({
contentType: 'application/json; charset=utf-8',
method: 'post',
async: false,
url: "Gateway/DB_Rola,
data:Obj,
success: function (Data) {alert("Good");},
error: function () {alert("Error");}
});
Upvotes: 0
Reputation: 8020
Try this It's just an Example,
function SearchProduct () {
var isExcluded = $('#chkisExcluded').is(':checked');
$.ajax({
type: "POST",
url: '@Url.Action("SearchProductManagement","ProductListManagement")',
data: { isExcluded: isExcluded,},
success: function (data) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
}
Controller
public JsonResult SearchProductManagement(bool isExcluded)
{
//Your code
return Json(data, JsonRequestBehavior.AllowGet);
}
View
<input type="button" value="Go" onclick="return SearchProduct();" class="button next" />
Upvotes: 0