how to fetch data from json action into view

Umair
[HttpPost] 
public JsonResult searchByName(string name)
{
    dbCRMEntities dbx = new dbCRMEntities();
    var test = name;             
    var names = dbx.CONTACTS.Where(chk => name == chk.NAME);
    return this.Json(names, JsonRequestBehavior.AllowGet);    
}

This method is returning the data in this format:

[
    {
        "CONTACT_ID": 37,
        "NAME": "umair",
        "JOB_TITLE": "internee",
        "COMPANY": "fastservices",
        "PHONE": "244",
        "EMAIL": "[email protected]",
        "WEB": "alskdjg",
        "ADDRESS": "lahore",
        "STATUS": "Inactive",
        "TAGS": "sdf",
        "LEAD_SOURCE": "partner",
        "BACKGROUND": "skldjga",
        "OWNER": "a",
        "BIRTHDAY": "2014-12-18",
        "EntityState": 2,
        "EntityKey": {
            "EntitySetName": "CONTACTS",
            "EntityContainerName": "dbCRMEntities",
            "EntityKeyValues": [
                {
                    "Key": "CONTACT_ID",
                    "Value": 37
                }
            ],
            "IsTemporary": false
        }
    }
]

and my jquery method is:

$(document).ready(function () {
    $("#btn1").click(function () {
        var name = $("#search").val();
        //name = "ali";
        alert(name);

        $.post("/Status/searchByName", { name: name }, function (data) {
            document.write(data);
            $.each(data, function (key, value) {
            });
        }, "text");
    });
});

I want to obtain data in tabular form in the view. Please guide me

charlietfl

Need to change dataType of $.post to 'json'. Then jQuery will return an array of objects as your data argument.

Now within your each the first argument will be index and the second will be the individual objects

$.post("/Status/searchByName", { name: name }, function (data) {

        $.each(data, function (index, item) {               
               var rowData =[];
                rowData.push(item.CONTACT_ID);
                rowData.push(item.COMPANY);
                rowData.push(item.EntityKey.EntitySetName);
                 /* ETC */

                var row ='<tr><td>' + rowData.join('</td><td>') +'</td></tr>';
                $('table').append(row);

        });
    }, "json");
});

You could also loop over each of the properties in each object using $.each and if it is a primitive value push it into the html

$.each(data, function (index, item) { 
     var rowData =[];
    $.each(item, function( key, value){
       if(typeof value !=='object'){
          rowData.push(value);
        }else if( key === 'EntityKey'){
           /* parse to data array*/
        }  
    });
    /* append to table as above */
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to fetch data from json action into view

From Dev

How to fetch data from JSON and show it in table cell view

From Dev

How to fetch data from json using Reactjs

From Dev

how to fetch data from json array in php

From Dev

How to fetch JSON data from DB?

From Dev

How use data from store in fetch action (react-redux)

From Dev

Data fetch from json

From Dev

Fetch the data from JSON response

From Dev

Fetch specific data from JSON

From Dev

Fetch the data from JSON response

From Dev

Backbone fetch Data from Json

From Dev

Fetch internal data from JSON

From Java

How to fetch data from local JSON file on react native?

From Dev

how to fetch json data from cassandra table using cqlsh

From Dev

How to iterate through JSON response from fetch() then add it to chart data?

From Dev

How to fetch huge JSON data from HTTP in Android?

From Dev

How to fetch data from json in android and display in RecyclerView?

From Dev

How to fetch json data from Mysql and convert it to an array in CodeIgniter

From Dev

How can fetch data from json through php?

From Dev

How to fetch data from core data and display in table View iOS 6

From Dev

How to show first data in table view while fetch data from api

From Dev

How to fetch data from an Array

From Dev

How to fetch data from firebase :

From Dev

reactively fetch new data from external source on action button

From Java

Fetch data from JSON local file in React

From Dev

Unable to fetch JSON data from Server

From Dev

Not able to fetch data as per id from JSON

From Dev

unable to fetch data from json file in angularjs?

From Dev

Fetch correct data from json file with postman

Related Related

HotTag

Archive