In response to the above post I think it needs this line instead of your line:-
var strMethodUrl = '@Url.Action("SubMenu_Click", "Logging")?param1='+value1+' ¶m2='+value2
Or else you send the actual strings value1 and value2 to the controller.
However, for me, it only calls the controller once. It seems to hit 'receieveResponse' each time, but a break point on the controller method shows it is only hit 1st time until a page refresh.
Here is a working solution. For the cshtml page:-
<button type="button" onclick="ButtonClick();"> Call »</button>
<script>
function ButtonClick()
{
callControllerMethod2("1", "2");
}
function callControllerMethod2(value1, value2)
{
var response = null;
$.ajax({
async: true,
url: "Logging/SubMenu_Click?param1=" + value1 + " ¶m2=" + value2,
cache: false,
dataType: "json",
success: function (data) { receiveResponse(data); }
});
}
function receiveResponse(response)
{
if (response != null)
{
for (var i = 0; i < response.length; i++)
{
alert(response[i].Data);
}
}
}
</script>
And for the controller:-
public class A
{
public string Id { get; set; }
public string Data { get; set; }
}
public JsonResult SubMenu_Click(string param1, string param2)
{
A[] arr = new A[] {new A(){ Id = "1", Data = DateTime.Now.Millisecond.ToString() } };
return Json(arr , JsonRequestBehavior.AllowGet);
}
You can see the time changing each time it is called, so there is no caching of the values...