gpt4 book ai didi

asp.net - 返回 JSON 列表并在 MVC 4 View 页面上使用

转载 作者:行者123 更新时间:2023-12-02 01:37:56 25 4
gpt4 key购买 nike

我已经使用 JSON 返回一个列表,但不确定如何在 MVC 4 View 页面上使用返回的列表。这可能吗?

查看页面

var subjectId = value;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/JobProfiles/FindJobProfiles/" + subjectId,
data: "{}",
dataType: "json",
success: function (data)
{

}
});

Controller

[HttpPost]
public ActionResult FindJobProfiles(int? subjectId)
{
if (subjectId.HasValue)
{
Subject subject = subjectRepository.Get(subjectId.Value);
IList<JobProfile> jobProfiles = jobProfileRepository.GetBySubject(subject.Id, false, false);
var jobProfileList = from c in jobProfiles select new { Id = c.Id, Title = c.Title };
return new JsonResult { Data = jobProfileList.ToList() };
}
else
{
return null;
}
}

查看页面显示

foreach (JobProfile job in jobProfiles)
{
<a href="/JobProfiles/View/@job.Id" title="@job.Title">@job.Title
}

返回了正确的数据,但不确定如何访问 View 页面上的列表并显示数据。

最佳答案

将 div 添加到页面以显示结果或要显示的 html 元素:

<div id="results" />

添加一个循环结果并将其附加的成功处理程序:

var subjectId = value;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/JobProfiles/FindJobProfiles/" + subjectId,
data: "{}",
dataType: "json",
success: function(data) {

$.each(data, function(index, item) {
$('#results').append('<a href"/JobProfiles/View/' + item.Id + '" title="' + item.Title +'">' + item.Title + '</a>');
});
}

});

关于asp.net - 返回 JSON 列表并在 MVC 4 View 页面上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29984081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com