简体   繁体   English

部分视图无法通过Ajax调用呈现

[英]Partial view not rendering through ajax call

I have a controller action which is getting called by JQuery ajax, and hopefully returniung the contents of "Results" to the requesting page. 我有一个由JQuery ajax调用的控制器操作,希望将“结果”的内容返回到请求页面。 So far I have... 到目前为止,我有...

Controller 控制者

    public ActionResult DynCalc(resultsModel model){
    ...
    //code that populates model - all working ok
    ...
     if (Request.IsAjaxRequest())
          {
            return PartialView("results", model.Results);
          }
          else
          {
            return null; //Handle this later
          }
    }

This passes back a valid model. 这将传回有效模型。 Called from the javascript: 从javascript调用:

     $.ajax({
            url: "/Test/DynCalc",
            type: "POST",
            data: $("#frmResults").serialize(), //This part works
            dataType: "html",
            success: function (result) {
                $('#resultsSection').html(result);
                $('#hide_panel a').flash("#111", 1000);
            }
        });

Sucess is never hit. 成功永远不会受到打击。 Hopefully somebody can just tell me i'm being daft and missing something obvious? 希望有人能告诉我我很傻并且缺少明显的东西吗?

My partial view results is just a HTML page with javascript on it. 我的部分视图结果只是带有javascript的HTML页面。 If I just have the following 如果我只有以下内容

Hello world in my results view, then it works ok, with any script in, it doesn't 在我的结果视图中, Hello world ,然后一切正常,在没有任何脚本的情况下,

Am I on the right track? 我在正确的轨道上吗? Should I be changing the type of return on the controller? 我应该更改控制器上的退货类型吗? Or using JSON? 还是使用JSON?

I didn't post the contents of the results page, as It doesn't matter if it's a whole document or simply <b>hi</b> - it does the same thing. 我没有发布结果页面的内容,因为它是整个文档还是简单的<b>hi</b>都没关系-它执行相同的操作。

Try to return just a View. 尝试只返回一个视图。 Because your result in this case is a complete View (because the action result could be HTML, XML, JSON...whatever). 因为在这种情况下,您的结果是完整的View(因为操作结果可能是HTML,XML,JSON等)。 Use PartialView only as a way to render part of your View. 将PartialView 用作呈现部分 View的方法。

Eg on your MasterPage you would like to "always" render user info: @RenderAction("UserInfoAction", "UserController") 例如,在您的MasterPage上,您希望“始终”呈现用户信息: @RenderAction("UserInfoAction", "UserController")

    var model= {
        "PropertyName1":$("#txt1").val(),
        "PropertyName1": $("#txt2").val(),         
       }

     $.ajax({
         type:"POST",
         url: 'Url.Action("DynCalc","ControllerName")',
         data: JSON.stringify(model),
         contentType: "application/json;charset=utf-8",
         success: function (data, status, xhr)
          {
         alert("The result is : " + status + ": " + data);
          },
         error: function (xhr)
         {
         alert(xhr.responseText);
         }
       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM