为什么我的膳食信息不显示在我的项目后,我追加数据表?



我创建了一个有Meal模型和cms控制器和webcms web控制器的订单食品项目,我从webcms创建了一个Index视图。我想显示所有的膳食信息在它作为一个表使用Ajax,但它只显示表的标题。我尝试使用httpget函数获取数据,它工作得很好。

My webcms controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using Delivary.Models;
using System.Web.Http;
namespace Delivary.Controllers.api
{
public class webcmsController : ApiController
{
ApplicationDbContext db;
public webcmsController()
{
db = new ApplicationDbContext();
}
[HttpGet]
public IEnumerable<Meal> GetMeals()
{
return db.MealTable;
}
[HttpGet]
public Meal GetoneMeal( int id)
{
var c = db.MealTable.SingleOrDefault(x => x.MealId == id);
return c;
}
}
}

MyMealmodel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Delivary.Models
{
public class Meal
{
public int MealId { get; set; }
public string MealName { get; set; }
public double MealPrice { get; set; }
public string MealImg { get; set; }
public string Cat { get; set; }
}
}

我的cms控制器索引视图

@{
ViewBag.Title = "Index";
}
<center>
<br />
<table id="meals_tbl" class="table table-bordered ">
<tr>
<th>MealId</th>
<th>MealName</th>
<th>MealPrice</th>
<th>MealImg</th>
<th>Cat</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</table>
</center>
@section scripts {
<script>
function showMeals() {
$.ajax({
url: "/api/webcms",
method: "GET",
dataType: "json",
success: function (data) {
$.each(data, function (k, v) {
var res = '<tr>';
res += '<td>' + v.MealId + '</td>';
res += '<td>' + v.MealName + '</td>';
res += '<td>' + v.MealPrice + '</td>';
res += '<td>' + v.MealImg + '</td>';
res += '<td>' + v.Cat + '</td>';
res += '<td> Edit/td>';
res += '<td> Delete/td>';
res += '</tr>';

$("#meals_tbl").append(res);
});
}
});
}
$(document).ready(function () {
showMeals();
});
</script>
}

在你的ajax代码更新URL与方法名称,即

$.ajax({
url: "/webcms/GetMeals",
...
});

最新更新