有没有理由说明为什么在MVC 1中工作的ajax不会出现在MVC3中?

Har*_*old 2 c# ajax asp.net-mvc jquery asp.net-mvc-3

你好,

我最近将我的项目从ASP.NET MVC 1 .NET 3.5 VS2008升级到ASP.NET MVC 3 .NET 4.0 VS2010.

除了我发现我运行的ajax的某个特定部分不再有效之外,其中大部分都没有用.

这是代码:

    var filterEntities = function () {
        $.get({
            url: "../../ProjectEntities.mvc/OfType/<%= Model.Change.Job.Quote.Project.Id %>?entityType=" + $("#ChangesForm select[name=ProjectEntityType]").val(),
            success: function (data) {
                response = projectSupport.parseJson(response);

                var entitySelect = $("#ChangesForm select[name=ProjectEntity]");
                entitySelect.empty();

                hasValues = (response.length > 0);

                for (var i in response) {
                    entitySelect.appendListItem(response[i].id, response[i].title);
                }

                updateEditLink();
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

那段代码继续打电话

public ActionResult OfType(int id, int entityType)
    {
        var project = projectService.Find(id);
        return Json(projectEntityService.ProjectEntitiesOfType(applicationService.ForProject(project), (EntityType)entityType).Select(entity => new { title = entity.Title + " (" + entity.Application.Description + ")", id = entity.Id }));
    }
Run Code Online (Sandbox Code Playgroud)

以前都很好用.任何人都有任何想法可能导致问题?我在网站的其他部分有ajax工作正常,所以我不认为我丢失了适当的jquery文件或任何东西.

谢谢,哈利

Mar*_*man 6

你需要设置JsonRequestBehavior.AllowGet你的return Json()

var data = projectEntityService.ProjectEntitiesOfType(applicationService.ForProject(project), (EntityType)entityType).Select(entity => new { title = entity.Title + " (" + entity.Application.Description + ")", id = entity.Id });

return Json(data, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)

这样做是为了防止Json Hijacking