2016-06-12 79 views
0

我有一個包含以下操作方法一ScoreDataModelsController:如何從ApiController訪問數據控制器/數據模型?

public ActionResult Getnames() 
     { 
      return View(db.ScoreDataModels.ToList()); 
     } 

在視圖中我有一個包含Getnames.cshtml相應ScoreDataModels文件夾:

@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> 

這一切工作正常。現在我想使用REST將這些數據(即名稱)作爲json/XML訪問。我曾設法讓ApiController與標準的環境中工作,並通過打開http://.../api/Andi我從字符串[]的數值以XML格式:

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) 
     { 
     } 
    } 
} 

現在,而不是「值1,值......」我喜歡從我的ScoreDataModel/ScoreDataModelsController獲取名稱。

ScoreDataModel看起來像這樣。我已經使用這個模型來創建Visual Studio中的控制器和視圖腳手架:

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; } 

    } 
} 

我將非常感激,如果你能帶我到正確的方向得到這個REST API和我現有的數據控制器的工作/數據模型。

回答

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; 
    } 
} 
+0

聽起來很有希望,謝謝。我可以在哪個命名空間中找到DatabaseContext? –

+0

這是您在MVC控制器中使用的「db」對象。實體框架數據庫上下文類。如果可以幫助您,請標記爲已回答 –

+0

如何訪問此對象?在我的ScoreDataModelsController中,上下文是用「private ApplicationDbContext db = new ApplicationDbContext()」創建的 - 我怎樣才能從DataRepository.cs訪問這個db變量? –

1

使用本

var data= db.ScoreDataModels.ToList() 

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

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

       } 


       return list; 
+0

發表評論如果需要任何幫助 –

+0

works perfec還有 - 非常感謝。我只需要創建Databese上下文'ApplicationDbContext db = new ApplicationDbContext();' –

相關問題