【问题标题】:calling json URL in MVC 4在 MVC 4 中调用 json URL
【发布时间】:2014-07-07 12:37:25
【问题描述】:

我制作了一个 ASP.NET WEB API,它以 json 格式获取客户和营养的详细信息。我在 IIS 上部署了我的 API,它工作正常。现在我想在我的 MVC 项目上使用该 API 的 URL。我的 URL 是这样的“http:/localhost:12/customernutrient/customernutrient?o=json”和类是这样的

public class Header
{    
    public string api_ver { get; set; }    
    public int req_type { get; set; }    
    public int code { get; set; }    
    public string description { get; set; }
}

public class Customer    
{        
    public int customerId { get; set; }    
    public string customerName { get; set; }    
}

public class Nutrient    
{    
    public int nutrientId { get; set; }    
    public string nutrientName { get; set;}    
}

public class Body    
{    
    public List<Customer> customer { get; set; }    
    public List<Nutrient> nutrient { get; set; }    
}

public class RootObject    
{    
    public Header header { get; set; }    
    public Body body { get; set; }     
}

如何在我的链接的帮助下调用我的 API 并以标签之类的方式显示内容。我想在不同的标签中显示这些数据

   {
    "header": {
        "api_ver": "1.2",
        "req_type": 1,
        "code": 1,
        "description": "Successful output"
    },
    "body": {
        "customer": [
            {
                "customerId": 1,
                "customerName": "Roundys1"
            },
            {
                "customerId": 2049,
                "customerName": "Test"
            }
        ],
        "nutrient": [
            {
                "nutrientId": 1,
                "nutrientName": "Calcium"
            },
            {
                "nutrientId": 2,
                "nutrientName": "Calories"
            },
            {
                "nutrientId": 3,
                "nutrientName": "Cholesterol"
            },
            {
                "nutrientId": 4,
                "nutrientName": "Dietary Fiber"
            },
            {
                "nutrientId": 5,
                "nutrientName": "Iron"
            },
            {
                "nutrientId": 6,
                "nutrientName": "Polyunsaturated Fat"
            },
            {
                "nutrientId": 7,
                "nutrientName": "Potassium"
            },
            {
                "nutrientId": 8,
                "nutrientName": "Protein"
            },
            {
                "nutrientId": 9,
                "nutrientName": "Saturated Fat"
            },
            {
                "nutrientId": 10,
                "nutrientName": "Sodium"
            },
            {
                "nutrientId": 11,
                "nutrientName": "Sugars"
            },
            {
                "nutrientId": 12,
                "nutrientName": "Total Carbohydrate"
            },
            {
                "nutrientId": 13,
                "nutrientName": "Total Fat"
            },
            {
                "nutrientId": 14,
                "nutrientName": "Vitamin A"
            },
            {
                "nutrientId": 15,
                "nutrientName": "Vitamin C"
            }
        ]
    }
}

请分享您可能的解决方案

【问题讨论】:

  • 显示你到目前为止尝试过的内容。
  • 我在 Web 表单中的 C# 代码的帮助下完成了这项工作,但我不知道如何在 Razor 中实现这一点
  • 那么你希望有人在这个问题中用剃须刀引擎教你 MVC 吗?

标签: asp.net asp.net-mvc json


【解决方案1】:

您可以使用 ajax 调用来填充您的视图。假设您要在单击 id = 'btnpopulatedata' 的按钮时填充数据

<script>
$(document).ready(function () {
var itemName = "#btnpopulatedata";
$(itemName).click(function () {
    $.ajax({
        type: 'GET',
        dataType: 'Json',
        url: 'http:/localhost:12/customernutrient/customernutrient?o=json',
        success: function (data) {
            //write your logic to parse Json and populate the view controls.
        },
        error: function () {
            alert("Error);
        },
      });
    });
  });
</script>

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2014-09-10
    • 2015-06-08
    • 2013-02-23
    • 1970-01-01
    • 2016-09-15
    相关资源
    最近更新 更多