【问题标题】:Return view use Ajax返回视图使用 Ajax
【发布时间】:2018-05-05 15:28:08
【问题描述】:

我想使用 Ajax 脚本调用视图:

在主视图中:

<script>
    var onCommand = function (column, command, record, recordIndex, cellIndex) {
       Ext.Msg.alert('record = ' + record.data.ID);
        Ext.Ajax.request({
            url: '/Details/',
            method: 'GET',
            params: {
                id: record.data.ID
            },

            success: function (response) {
                var result = (response.responseText);
                if (result != "") {
                    modelName = result;
                    CreateLookUp(combo, id, false, true);
                } else {

                    CreateLookUp(combo, id, true, false);
                }
            }
        });

            }
</script>

控制器:

  // GET: Bob/Details/5
        public ActionResult Details(String ID)
        {
           int  id = Convert.ToInt32(ID);
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            BobRepository bobRepository = new BobRepository();
            Bob bob = bobRepository.GetBob(id);
            if (bob == null)
            {
                return HttpNotFound();
            }
            return View(bob);
        }

调用控制器的函数调用,不返回turget视图。是什么原因?

【问题讨论】:

  • 在成功处理程序中执行console.log(response) 并检查它是否是预期的输出。此外,您应该为 ajax 调用返回部分视图结果。
  • 是的。响应返回所有 html 代码。
  • 那你的意思是turget view没有返回???
  • 在浏览器中,我停留在旧视图上。我需要切换到一个新的,其代码已在答案中返回。
  • 如果你想切换到一个全新的视图,你为什么要进行 ajax 调用?为什么不提交普通表单并让服务器返回重定向结果? ajax 的全部意义在于为用户提供部分页面更新。看起来您正在调用其他方法CreateLookUp。我们不知道里面是什么以及它应该做什么。只有你知道!

标签: asp.net-mvc asp.net-ajax ext.net


【解决方案1】:

我不确定您要做什么,我假设您正在某个弹出模式中填充该视图,首先您需要返回部分视图而不是视图,我建议检查请求 is ajax 然后返回部分视图,否则返回视图, 像这样的

if(Request.IsAjaxRequest())
{return PartialView(bob);}
else
return View(bob);

在你的js中,将文本解析成html,可以使用jquery.htmlparse

  success: function (response) {
                var result =  $.parseHTML(response);
                //do what you want with your html

【讨论】:

  • 我正在尝试调用 GridVew 的演示文稿。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多