*首先,我想知道是否必須爲控制器中的每個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事件已經用過的東西不工作取下入圍客戶,我需要它的工作讓選擇的客戶可以將其刪除。
您確定'CustomerCollection.Remove(selectedCustomer);'從數據庫中刪除客戶,而不僅僅是從集合中?如果你沒有視圖,你可以調用'返回內容(「你好!」);'而不是'返回視圖();' –
@ keiv.fly您好我不使用數據庫即時製作對象並將它們存儲在列表中並從列表中將它們展示出來 – SINFER
那麼列表如何在頁面間持續存在?列表在哪裏創建? –