Partial view ignoring dataset and not displaying

I am developing an application in ASP.NET MVC. I am working on a page that takes some user-entered values ​​and then retrieves a dataset from them using a stored procedure. My preference is that the data is entered on the same page as on the display, so I use AJAX and partial views for this. My code worked fine with a dummy set of simple data (a simple line), but now when I use a more advanced data set (Ienumerable), it no longer displays a partial view.

Here is part of my view (the text fields into which data is entered are hidden for length purposes):

<!--SEARCH RESULTS PARTIAL FILLED BELOW-->
<div id="search-results">

</div>
<!---->


<script>
    function getSearchResults() {

        var SqlViewModel = {
           //assign textboxes to values to pass to controller (hidden)
        }

        $.ajax({
            type: "POST",
            data: JSON.stringify(SqlViewModel),
            dataType: "json",
            contentType: "application/json",
            url: "/Sql/Search/",
            success: function(result) {
                $('#search-results').html(result);
            }
        });        }


</script>

, "", ajax. ( SqlVieWModel)

:

[HttpPost]
    public ActionResult Search(SqlViewModel sT)
    {
             //code for stored procedure

        var sql = //get stored procedure and parameters         

        SqlPartialViewModel obj = new SqlPartialViewModel();
        obj.SqlData = sql; //obj.SqlData is of type IEnumerable<Get_Result>

        return PartialView("_SearchResultsPartial", obj);

SqlPartialViewModel:

 public class SqlPartialViewModel
{
    public IEnumerable<Get_Result> SqlData { get; set; }
}

, (_SearchResultssPartial.cshtml):

 @model SqlPartialViewModel

<table>
<tr>
   <th>//Display stuff</th>

</tr>
@foreach(var result in Model.SqlData)
{ 
<tr>
  <td>//Display stuff</td>
</tr>
}

, .

+4
1

Ajax json :

$.ajax({
        type: "POST",
        data: JSON.stringify(SqlViewModel),
        dataType: "json", <---- HERE
        contentType: "application/json", 
        url: "/Sql/Search/",
        success: function(result) {
            $('#search-results').html(result);
        }
});

PartialView ActionResult, html, json.

dataType "html" ( javascript ).

+4

All Articles