2014-05-25 58 views
-1

*首先,我想知道是否必須爲控制器中的每個Action方法提供一個視圖?從按鈕調用MVC操作?

*如何在另一個視圖中單擊按鈕時在MVC4中調用Action方法?我是否需要爲Action方法提供一個視圖,通過按下另一個視圖中的按鈕來調用該方法。

這裏是我的代碼

CustomerController

public class CustomerController : Controller 
    { 
     // 
     // GET: /Customer/ 
     List<Customer> CustomerCollection = new List<Customer>(); 
     public CustomerController() 
     { 

      Customer cus = new Customer(); 
      cus.CustomerId = 1; 
      cus.Name = "dath"; 
      cus.Gender = "Male"; 
      cus.City = "Csmbo"; 
      CustomerCollection.Add(cus); 

      cus = new Customer(); 
      cus.CustomerId = 2; 
      cus.Name = "Jacob"; 
      cus.Gender = "FeMale"; 
      cus.City = "Cosbo"; 
      CustomerCollection.Add(cus); 

      cus = new Customer(); 
      cus.CustomerId = 3; 
      cus.Name = "Gags"; 
      cus.Gender = "Male"; 
      cus.City = "NewYork"; 
      CustomerCollection.Add(cus); 
     } 
     public ActionResult GetCustomerList() 
     { 

      return View(CustomerCollection); 
     } 
     public ActionResult GetCustomer(int id) 
     { 
      var selectedCustomer = CustomerCollection.Where(p => p.CustomerId == id).FirstOrDefault(); 
      return View(selectedCustomer); 
     } 

這是操作方法我也會通過按下叫DeleteCustomer視圖按鈕調用。這個動作方法我還沒有創建任何視圖

  [HttpPost] 
      public ActionResult DeleteCus(int id) 
      { 
       var selectedCustomer = CustomerCollection.Where(o => o.CustomerId == id).FirstOrDefault(); 
       CustomerCollection.Remove(selectedCustomer); 
       RedirectToAction("GetCustomerList", "Customer"); 
       return View(); 
      } 

這是DeleteCustomer操作方法

public ActionResult DeleteCustomer(int id) 
     { 

      var selectedCustomer = CustomerCollection.Where(a => a.CustomerId == id).FirstOrDefault(); 

      return View(selectedCustomer); 
    } 

最後這是一個即時通訊傳遞DeleteCustomer行動入圍客戶的DeleteCustomer視圖方法。按鈕在這個視圖中。從這個按鈕我需要調用DeleteCus操作方法(因此它會從customerCollection列表

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<myapp12.Models.Customer>" %> 

<!DOCTYPE html> 

<html> 
<head runat="server"> 
    <meta name="viewport" content="width=device-width" /> 
    <title>DeleteCustomer</title> 
</head> 
<body> 
    <h3>Are you sure you want to delete this?</h3> 
    <fieldset> 
     <legend>Customer</legend> 

     <div class="display-label"> 
      <%: Html.DisplayNameFor(model => model.Name) %> 
     </div> 
     <div class="display-field"> 
      <%: Html.DisplayFor(model => model.Name) %> 
     </div> 

     <div class="display-label"> 
      <%: Html.DisplayNameFor(model => model.Gender) %> 
     </div> 
     <div class="display-field"> 
      <%: Html.DisplayFor(model => model.Gender) %> 
     </div> 

     <div class="display-label"> 
      <%: Html.DisplayNameFor(model => model.City) %> 
     </div> 
     <div class="display-field"> 
      <%: Html.DisplayFor(model => model.City) %> 
     </div> 
    </fieldset> 
    <% using (Html.BeginForm()) { %> 
     <p> 
      <input type="submit" value="Delete" onclick="location.href='@Url.Action("DeleteCus", "Customer", new { id = Model.CustomerID })'"/> 
    <%: Html.ActionLink("Back to List", "GetCustomerList")%> 
     </p> 
    <% } %> 

</body> 
</html> 

我在onClick事件已經用過的東西不工作取下入圍客戶,我需要它的工作讓選擇的客戶可以將其刪除。

+0

您確定'CustomerCollection.Remove(selectedCustomer);'從數據庫中刪除客戶,而不僅僅是從集合中?如果你沒有視圖,你可以調用'返回內容(「你好!」);'而不是'返回視圖();' –

+0

@ keiv.fly您好我不使用數據庫即時製作對象並將它們存儲在列表中並從列表中將它們展示出來 – SINFER

+0

那麼列表如何在頁面間持續存在?列表在哪裏創建? –

回答

0

你應該存儲你的數據在數據庫中,否則如果在控制器的構造函數的集合是創建你的數據不能保存更改。

應該頁面時都死了給定和創建每次頁面加載。數據庫數據和memcached之類的東西在頁面調用之間保持不變,通常的變量不會。

這是沒有查看的代碼。但是,只要CustomerCollection是通常的變量,remove就不會起作用。

[HttpPost] 
public ActionResult DeleteCus(int id) 
{ 
    var selectedCustomer = CustomerCollection.Where(o => o.CustomerId == id).FirstOrDefault(); 
    CustomerCollection.Remove(selectedCustomer); 
    return RedirectToAction("GetCustomerList", "Customer"); 
} 
+0

你能給我一個簡單的代碼來調用一個按鈕的動作嗎 – SINFER

+0

好吧謝謝但爲什麼返回一個RedirectToAction?我知道如果沒有對此的看法不能返回一個視圖。但爲什麼返回一個redirectToAction? – SINFER

+0

由於不是返回一個普通頁面,而是使用'HTTP/1.1 301返回頁面永久移動 位置:http://www.example.org/ Content-Type:text/html Content-Length:174 ... ' –