jQuery ajax response bind into MVC Model table

Dayan

I'm trying to push an ajax response array into MVC table. This is how my script looks like:

<script type="text/javascript">

    $(document).ready(function () {
        $('#form1').submit(function (event) {
            event.preventDefault();
            event.returnValue = false;
            var selectValue = $('#selectValue').val();

            $.ajax({
                url: "/api/Admin/GetDepotDetails/",
                type: "POST",
                data: { "selectValue": selectValue },
                dataType: "json",
                success: function (data) {
                    $("#Grid").html($(data).children());
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    debugger;
                    alert(textStatus, errorThrown, jqXHR);
                }
            });
        });
    });



</script>

This is how my partial view looks like:

@model IEnumerable<SampleApp.DepotDetail>

<table class="table table-condensed" id="Grid">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DepotID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ColumnName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CountryCode)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="warning">
            <td>
                @Html.DisplayFor(modelItem => item.DepotID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ColumnName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CountryCode)
            </td>
        </tr>
    }

</table>

This is how the WebApi method looks like:

[HttpPost]
public IEnumerable<DepotDetail> GetDepotDetails(Selected selectValue)
{
    var model = depotsDetails.Where(x => x.ColumnName == selectValue.SelectValue) as IEnumerable<DepotDetail>;
    var viewModel = new SearchViewModel{ DepotDetailsList = model, ActionLists = new ActionList()} ;
    return model;
}

This is how the View looks like:

@model IEnumerable<SiemensSampleApp.DepotDetail>
<div class="row">
    <div class="col-md-4">
        <form id="form1">
            @*@Html.DropDownListFor(model => model.DepotListSelectValue, Model.DepotLists, new { @class = "form-control" })*@

            @Html.DropDownList("selectValue", new List<SelectListItem>
            {
                new SelectListItem() {Text = "Depot ID", Value="Depot ID"},
                new SelectListItem() {Text = "Depot Name", Value="Depot Name"},
                new SelectListItem() {Text = "Address", Value="Address"}
            }, new { @class = "selectValue", @id = "selectValue" })
            @*//, new { @class = "chzn-select", @id = "chzn-select" }*@





            <input type="submit" value="submit" />
        </form>
        <br /><br /><br />
        <table id="records_table" style="width:100%;"></table>

        <div id="tableHere">
            @{
                if (Model == null)
                {
                    Html.RenderPartial("_SearchResult", new List<DepotDetail>() { });
                }
            }


        </div>

    </div>

</div>

Question: Through WebApi i'm trying to get a List of details and bind it to a MVC table. What is the best way to do this? I have used

$("#Grid").html($(data).children());

To fill the Grid. But the table doesn't have any data. can someone please give me an idea how to fill the grid using above Partial view. Thank you in advance!

Shyju

Your web api endpoint return data ( in json format), not the HTML markup from your partial view. You have 2 options.

1) Create an mvc action method which gets the data and pass it to your partial view and return the partial view response and use that to update your UI

[HttpPost]
public IEnumerable<DepotDetail> GetDepotDetails(Selected selectValue)
{
    var model = depotsDetails.Where(x => x.ColumnName == selectValue.SelectValue) 
                                                             as IEnumerable<DepotDetail>;

    return PartialView(model);
}

Now make sure you have a partial view called GetDepotDetails.cshtml in ~/Views/Shared or ~/View/YourControllerName/. This view should be strongly typed to a collecction of DepotDetail.

@model IEnumerable<DepotDetail>

<p>Here loop through each item in Model and render the table</p>
<p> Same as your partial view in the question </p>

And in your success event,

success: function (data) {
               $("#Grid").html(data);
         },

2) Use your current web api endpoint and read the data in your ajax method's success event and dynamically build the html markup for the table rows and set that as the content of your Grid table.

success: function (data) {
             var tblHtml="";
             $.each(data.DepotDetailsList,function(a,b){

                       tblHtml+= "<tr><td>"+b.DepotID+"</td>";
                       tblHtml+= "<td>"+b.ColumnName+"</td>";
                       tblHtml+= "<td>"+b.Country+"</td>";
                       tblHtml+= "<td>"+b.CountryCode+"</td>M/tr>";

             });
             $("#Grid > tbody").html(tblHtml);
         },

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot Bind JSON response to textbox using jquery ajax (MVC 4)

From Dev

How to bind response of Ajax request to Model in EmberJs?

From Dev

spring mvc response not loading in jquery ajax call

From Dev

ASP.Net MVC Model wont bind on ajax post

From Dev

MVC 4 Ajax.BeginForm POST does not bind to model in conroller

From Dev

jQuery manipulating table rows from ajax response

From Dev

JQuery bind on Ajax success

From Java

Using jQuery to build table rows from AJAX response(json)

From Dev

How to set AJAX response to jquery data table columns?

From Dev

How to re-create jQuery data-table on ajax response?

From Dev

ASP.NET MVC Model Binding with jQuery ajax request for collection

From Dev

How to pass formData(fileupload) through jquery ajax along with MVC model

From Dev

How to Bind Model Class in MVC

From Dev

Mvc Checkboxes Bind with model Bool

From Dev

Ajax data into MVC model

From Dev

Post an MVC model with AJAX?

From Dev

MVC send Model with ajax

From Dev

Knockout MVC, bind model & add object to model

From Dev

Add row in table with jquery which is connected with model in MVC

From Dev

using jquery each method and get data from table MVC Model

From Dev

jQuery ajax cache response

From Dev

jQuery - response of ajax

From Dev

Toggling an AJAX Response in jQuery

From Dev

jQuery - response of ajax

From Dev

AJAX response not appending with JQuery

From Dev

jquery unbind and bind on ajax success

From Dev

jquery ajax and this, where to apply bind

From Dev

Dynamic Jquery table constructed using ajax response data on textbox's each keyup event, not showing data of last ajax response (last keyup)

From Dev

Reload table after ajax response

Related Related

HotTag

Archive