如何从ApiController访问数据控制器/数据模型?

时间:2016-06-12 08:18:31

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

我有一个ScoreDataModelsController,它包含以下操作方法:

@model IEnumerable<WebApplication1.Models.ScoreDataModel>

@{
    ViewBag.Title = "Get Names";
    Layout = "~/Views/Shared/_emptyLayout.cshtml";
}

<table class="table">

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)

            </td>
        </tr>
    }

</table>

在视图中,我有相应的ScoreDataModels文件夹,其中包含Getnames.cshtml:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;


namespace WebApplication1.Controllers
{
    public class AndiController : ApiController
    {

        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2", "und en dritte" };

  //Here I need help: ScoreDataModelsController sdm = new ScoreDataModelsController();
           // var res = from r in sdm


        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

一切正常。现在我想使用REST将这些数据(即Names)作为json / XML访问。我设法让ApiController使用标准设置并打开SignalR on Android我从XML格式的字符串[]中获取值:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class ScoreDataModel
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public int Score { get; set; }

    }
}

现在,而不是&#34; value1,value2 ...&#34;我想从我的ScoreDataModel / ScoreDataModelsController中获取名称。

ScoreDataModel看起来像这样。我已经使用这个模型在Visual Studio中通过脚手架创建控制器和视图:

return Board.findOne({ boardId: id }).then(function(data) {
  if (! data) throw new Error('data empty');
  return data.children;
}).catch(function(err) {
  console.log(err);
});

如果您能引导我使用现有的数据控制器/数据模型来使用这个REST API,我将非常感激。

2 个答案:

答案 0 :(得分:1)

使用此

var data= db.ScoreDataModels.ToList()

                List<String>list=new List<String>();

                foreach(var r in data)
                {
                    list.add(r.Name);

                }


                return list;

答案 1 :(得分:1)

创建一个保存数据访问逻辑的中心类,如下所示:

public class DataRepository
{
    private DatabaseContext db = new DatabaseContext();

    public List<ScoreDataModel> GetNames()
    {
        return db.ScoreDataModels.ToList();
    }
}

现在您可以使用此类从MVC控制器和api控制器访问您的数据:

public class AndiController : ApiController
{
    private DataRepository dbRepo = new DataRepository();

    public IEnumerable<ScoreDataModel> Get()
    {
        List<ScoreDataModel> names = dbRepo.GetNames();
        return names;
    }
}
相关问题