简体   繁体   中英

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. 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:

     $.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. If I just have the following

Hello world in my results view, then it works ok, with any script in, it doesn't

Am I on the right track? Should I be changing the type of return on the controller? Or using 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.

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). Use PartialView only as a way to render part of your View.

Eg on your MasterPage you would like to "always" render user info: @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);
         }
       });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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