首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从部分视图FormMethod.Get返回JSON数据?

如何从部分视图FormMethod.Get返回JSON数据?
EN

Stack Overflow用户
提问于 2013-10-24 05:52:17
回答 1查看 230关注 0票数 0

我有以下代码发布到我的搜索Json。问题是url重定向到json搜索并显示原始json数据。我想返回到partialView中的一个表。对如何实现这一点有什么想法吗?

代码语言:javascript
复制
<div>
@using (Html.BeginForm("Search", "Home", Formmethod.Get, new {id="search-form"})){
    ...
    <button id="search-btn">Search</button>
}
</div>
<div>
    <table id="search-results">...</table>
</div>

我的家用控制器工作正常,但为了确保画面清晰...

代码语言:javascript
复制
public JsonResult Search(/*variables*/)
{
    ...
    return Json(response, JsonRequestBehavior.AllowGet);
}

然后我被重定向到“Search/(我的所有变量)”

EN

回答 1

Stack Overflow用户

发布于 2013-10-24 05:55:49

您的响应数据应该放入模型中,将模型传递给局部视图,并从控制器返回局部视图。

代码语言:javascript
复制
public PartialViewResult Search(/*variables*/)
{
    ...
    YourModel model = new YourModel();
    // Populate model
    return PartialView("_YourPartialView", model);
}

如果只想刷新局部视图,那么可以通过ajax调用控制器操作,并在ajax回调中调用$('#yourDivContainingThePartialView').html(response)

代码语言:javascript
复制
$.ajax({
  url: urlToYourControllerAction,
  method: 'get', // or 'post', depending on whether your controller action has side effects
  success: function (response) {
      $('#yourDivContainingThePartialView').html(response);
  }
  // other ajax options here if you need them
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19552986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档