I want to take data from function "NewData()" and display it in "SomeActionMethod View" by json. The problem is how to access the data in $.ajax This is my HomeController file
public ActionResult SomeActionMethod()
{
return View();
}
[HttpGet]
public JsonResult NewData()
{
List<mydatasample> mydata = new List<mydatasample>();
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test2", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
mydata.Add(new mydatasample { bookName = "test1", publisherName = "yum3", publishYear = 2018 });
int rowcount= mydata.Count;
return Json(mydata, JsonRequestBehavior.AllowGet);
}
public class mydatasample
{
public string bookName { get; set; }
public string publisherName { get; set; }
public int publishYear { get; set; }
}
This is my "Some Action Method View" file. In my $.ajax part what should i do that it must display data in some action method...
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
var hosturl = "http://" + window.location.hostname + ':' + window.location.port + "/Home/NewData";
console.log(hosturl);
$.ajax(
{
cache: false,
type: "GET",
url: hosturl,
success: function (e)
{
alert('i am in this function');
for (var i = 0; i > 10; i++)
{
console.log(e.mydata[])
mydata++;
}
},
error: function ()
{
alert('Failed to retrieve books.');
}
});
});