JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 3199

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.

Upvotes: 1

Views: 2036

Answers (2)

Gaurav Dubey
Gaurav Dubey

Reputation: 370

    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);
         }
       });

Upvotes: 0

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123901

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.

E.g. on your MasterPage you would like to "always" render user info: @RenderAction("UserInfoAction", "UserController")

Upvotes: 1

Related Questions