我有一个 Ajax ActionLink:
@Ajax.ActionLink("Science Package", "GetScience", new AjaxOptions { OnSuccess = "ShowData" })
整个想法是,单击链接时,它将从 GetScience 方法(返回对象列表)中获取所有数据并将其显示在 div 中。问题是如何做到这一点?我正在尝试使用 OnSuccess 方法来捕获结果,循环这些值并将它们显示在视图中。但这似乎不起作用。请帮忙?
我有一个 Ajax ActionLink:
@Ajax.ActionLink("Science Package", "GetScience", new AjaxOptions { OnSuccess = "ShowData" })
整个想法是,单击链接时,它将从 GetScience 方法(返回对象列表)中获取所有数据并将其显示在 div 中。问题是如何做到这一点?我正在尝试使用 OnSuccess 方法来捕获结果,循环这些值并将它们显示在视图中。但这似乎不起作用。请帮忙?
@soumadeep 根据讨论,我相信您的情况下的日期检索技术必须如下所示
控制器 :
public JsonResult GetScience()
{
var courseModel _db.Courses.ToList();
return Json(courseModel, JsonRequestBehavior.AllowGet);
}
查看JS:
function ShowData(result) {
console.log(result);
}
Just to update everything in a nutshell, after grilling for a lot of time & @kundan's suggestions did what i wanted to acheive. The details are below:
**The link:**
@Ajax.ActionLink("Science Package", "GetScience", new AjaxOptions{OnSuccess = "UpdateData" })
**The Action:**
public JsonResult GetScience()
{
var courseModel = _db.Courses.ToList();
// return "";
return Json(courseModel, JsonRequestBehavior.AllowGet);
}
**The JS:**
<script type="text/javascript">
function UpdateData(data) {
var target = $("#Context");
target.empty();
for (var i = 0; i < data.length; i++) {
var product = data[i];
target.append("<ul><b><ul>" + product.Name + "</ul></b><ul>" + product.Description + "</ul><b><ul>" + product.FacultyName + "</ul></b><ul>" + product.Price+ "</ul></ul>");
}
}
</script>
**The JS includes in layout file:**
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
The above steps will help you make async calls to Action methods & return results on the same page/ partial views.
Question, let me know by mail. Will be more than happy to get grilled :)