MVC3 部分视图,ajax 仅返回 JSON

发布于 2025-01-04 16:57:25 字数 1314 浏览 0 评论 0原文

我是网络编程新手,正在学习 MVC、ajax、jQuery 等,另外我正在使用 telerik 的一些控件。

我被困在一个特定的点上。

我有一个部分视图,其唯一目的是显示 Telerik 网格中上传的文件列表。当我第一次加载页面时它呈现良好。我这样做:

@Html.Partial("~/Views/Document/Lista.cshtml", Model.Files)

但是,在完成ajax文件上传后,我想通过ajax调用使用该部分视图更新特定的div:

$("#List").load("/Document/List/", { id: fk });

我倾向于使用POST调用,因为telerik网格需要一些json格式的数据,并且json与 GET 配合使用效果不佳。

现在这是我的控制器操作:

[GridAction][HttpPost]
public ActionResult List(int id)
{
    Document doc = db.Documents.Where(d => d.ID == id).Single();
    return PartialView("~/Views/Document/List.cshtml", new GridModel(doc.Files));
}

现在,这将仅生成 json。再次,当我第一次渲染它时,一切都很好,所以视图不应该是罪魁祸首。我正在查看 fiddler 中的响应,它看起来像这样:

{"data":[{"ID_File":54,"FK_Document":2,"FileName":"ckeditor_3.6.2.zip","FileType":"application/zip","FileData":null,"FileSize":2294443,"UploadingUser":null},{"ID_File":63,"FK_Document":2,"FileName":"254371_DropDownInGrid-CL.zip","FileType":"application/zip","FileData":null,"FileSize":1708740,"UploadingUser":"S-1-5-21-3024387492-927407738-3508986423-513"}],"total":14}

就像这样,没有任何 HTML。

我尝试在操作方法中使用各种选项,例如将返回类型更改为 JSON,或者返回整个视图而不是部分视图,但我总是得到相同的结果:裸 JSON。

我用谷歌搜索,但找不到类似的问题......

所以,我请求你的帮助。

I am new to web programming and i am learning MVC, ajax, jQuery, etc, plus i am using some controls from telerik.

And I am stuck at one particular point.

I have a partial view which only purpose is to show a list of uploaded files in telerik grid. And it renders fine when i am loading page for the first time. I do it like this:

@Html.Partial("~/Views/Document/Lista.cshtml", Model.Files)

But, upon completion of ajax file upload, i would like to update specific div with that partial view with an ajax call:

$("#List").load("/Document/List/", { id: fk });

I am inclined to use POST call because telerik grid requires some data in json format, and json doesn't work well with GET.

Now this is my controller action:

[GridAction][HttpPost]
public ActionResult List(int id)
{
    Document doc = db.Documents.Where(d => d.ID == id).Single();
    return PartialView("~/Views/Document/List.cshtml", new GridModel(doc.Files));
}

And now, this will result in json ONLY. Once again, when I render it for the first time, everyting is ok, so the view should't be the culprit. I am looking at the response in fiddler and it looks something like this:

{"data":[{"ID_File":54,"FK_Document":2,"FileName":"ckeditor_3.6.2.zip","FileType":"application/zip","FileData":null,"FileSize":2294443,"UploadingUser":null},{"ID_File":63,"FK_Document":2,"FileName":"254371_DropDownInGrid-CL.zip","FileType":"application/zip","FileData":null,"FileSize":1708740,"UploadingUser":"S-1-5-21-3024387492-927407738-3508986423-513"}],"total":14}

Just like this, no HTML whatsoever.

I tried playing with various options in my action method like changing return type to JSON, or returning whole view instead of partial but i always get same result: bare JSON.

I googled it, but couldn't find similar problem nowhere...

So, I am asking your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2025-01-11 16:57:25

您可能需要考虑刷新 telerik 网格而不处理 json 内容。
看:
http:// /www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-client-api-and-events.html#rebind

You may want to consider refreshing telerik grid without dealing with json stuff.
See:
http://www.telerik.com/help/aspnet-mvc/telerik-ui-components-grid-client-api-and-events.html#rebind

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文