从Button调用MVC动作?

时间:2014-05-25 06:47:55

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

*首先,我想知道是否必须为控制器中的每个Action方法创建一个视图?

*如何在另一个视图中单击按钮时在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的视图中的按钮来调用的Action方法。对于此Action方法,我还没有创建任何View

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

这是DeleteCustomer Action方法

public ActionResult DeleteCustomer(int id)
        {

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

            return View(selectedCustomer);
    } 

最后,这是我在DeleteCustomer Action方法中选中的客户选择的DeleteCustomer视图。按钮就在这个视图中。从这个按钮我需要调用DeleteCus Action方法(因此它将从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事件中使用的东西不起作用。我需要它才能工作,以便删除选定的客户。

1 个答案:

答案 0 :(得分:0)

您应该将数据存储在数据库中,否则无法保存更改。

如果您的数据是在控制器构造函数中的集合中创建的,那么每次加载页面时都会在页面被给定和创建时死亡。数据库数据和memcached之类的东西在页面调用之间持续存在,而通常的变量则没有。

这是没有View的代码。但是,只要CustomerCollection是常用变量,remove就不会起作用。

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