2016-06-09 219 views
0

大家好我是新來的。我有一個小問題,這是我的HomeController,我將使用Web API進行獲取和創建,但是我無法找到並編寫編輯/詳細信息和刪除。Web Api和ASP.Net MVC

public class HomeController : Controller 
{ 
    // GET: Home 
    public ActionResult Index() 
    { 

     List<Product1Model> lst =new List<Product1Model>(); 
     HttpClient Client = new HttpClient(); 
     Client.BaseAddress = new Uri("http://localhost:19440/"); 

     Client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 
     HttpResponseMessage responsive = Client.GetAsync("api/Product1").Result; 
     if (responsive.IsSuccessStatusCode) 
     { 
      lst = responsive.Content.ReadAsAsync<List<Product1Model>>().Result; 
     } 
     return View(lst); 
    } 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Create(Product1Model model) 
    { 
     HttpClient Client = new HttpClient(); 
     Client.BaseAddress = new Uri("http://localhost:19440/"); 

     Client.PostAsJsonAsync<Product1Model>("api/Product1",model) 
      .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()); 
     return RedirectToAction("index"); 
    } 

} 

回答

0

鑑於你只需要沖洗的重複當前正在使用中創建和索引編輯和刪除模式的例子目前的結構。

假設你遵循慣例,

對於需要在Views/Home/Detail.cshtml來創建視圖,然後添加動作的方法來處理GET和POST請求的詳細信息/編輯...

//GET Home/Detail/5 
public async Task<ActionResult> Detail(int id) { 
    var client = new HttpClient(); 
    client.BaseAddress = new Uri("http://localhost:19440/"); 
    client.DefaultRequestHeaders 
     .Accept 
     .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    string endpoint = string.Format("api/Product1/{0}", id); 
    var response= await client.GetAsync(endpoint); 
    if (response.IsSuccessStatusCode) { 
     var model = response.Content.ReadAsAsync<Product1Model>(); 
     return View(model); 
    } 
    return HttpNotFound(); 
} 

//POST Home/Detail/5 
[HttpPost] 
public async Task<ActionResult> Detail(int id, Product1Model model) { 
    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri("http://localhost:19440/"); 
    string endpoint = string.Format("api/Product1/{0}", id); 
    await client.PostAsJsonAsync<Product1Model>(endpoint, model) 
     .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()); 
    return RedirectToAction("Index"); 
} 

對於刪除它有點棘手,因爲您必須從視圖中決定使用哪種Http方法。然而你對web api的調用可能會期待DELETE方法。你可以堅持公約,並使用HttpDelete一路像...

//DELETE Home/Delete/5 
[HttpDelete] 
public async Task<ActionResult> Delete(int id) { 
    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri("http://localhost:19440/"); 
    client.DefaultRequestHeaders 
     .Accept 
     .Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    string endpoint = string.Format("api/Product1/{0}", id); 
    var responsive = await client.DeleteAsync(endpoint); 
    if (responsive.IsSuccessStatusCode) { 
     return RedirectToAction("Index"); 
    } 

    return HttpNotFound(); 
} 

你需要確保發出請求

當客戶端(視圖)使用正確的HTTP方法
0
public ActionResult Edit(YourModel model) 
{ 
    call your edigint logc 
    return View(); 
} 
public ActionResult Details(int itemId) 
    { 
     YourModel = new ModelType(); 
     YourModel = Call your Detail Logic 

     return view(YourModel )   
    }