2012-12-29 32 views
0

我是新來的Asp.net MVC和我有以下代碼的問題。如何觸發Http發佈行動

@model SportsStore.Domain.Entities.ShippingDetails 

@{ 
    ViewBag.Title = "SportsStore: Checkout"; 
} 

<h2>Check out now</h2> 
Please enter your details and we'll send your goods right away! 
@using (Html.BeginForm("Checkout", "Cart")) 
{ 
    @Html.ValidationSummary() 

    <h3>Ship to</h3> 
    <div>Name: @Html.EditorFor(x => x.Name)</div> 

    <h3>Address</h3> 
    <div>Line 1: @Html.EditorFor(x => x.Line1)</div> 
    <div>Line 2: @Html.EditorFor(x => x.Line2)</div> 
    <div>Line 3: @Html.EditorFor(x => x.Line3)</div> 
    <div>City: @Html.EditorFor(x => x.City)</div> 
    <div>State: @Html.EditorFor(x => x.State)</div> 
    <div>Zip: @Html.EditorFor(x => x.Zip)</div> 
    <div>Country: @Html.EditorFor(x => x.Country)</div> 

    <h3>Options</h3> 
    <label> 
    @Html.EditorFor(x => x.GiftWrap) 
    Gift wrap these items 
    </label> 


     <p align="center"> 
      <input class="actionButtons" type="submit" value="Complete order"/>   
     </p> 


} 

我期望類型的輸入提交給調用從我的控制器POST版本結帳的動作,下面

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using SportsStore.Domain.Abstract; 
using SportsStore.Domain.Entities; 
using SportsStore.WebUI.Models; 

namespace SportsStore.WebUI.Controllers 
{ 
    public class CartController : Controller 
    { 
     private IProductsRepository repository; 
     private IOrderProcessor orderProcessor; 

     public CartController(IProductsRepository repo, IOrderProcessor proc) 
     { 
      repository = repo; 
      orderProcessor = proc; 
     } 

     [HttpPost] 
     public ViewResult Checkout(ShippingDetails shippingDetails, Cart cart) 
     { 
      var test = Request.Form["Line1"]; 
      if (cart.Lines.Count() == 0) 
      { 
       ModelState.AddModelError("", "Sorry, your cart is empty!"); 
      } 
      if (ModelState.IsValid) 
      { 
       orderProcessor.ProcessOrder(cart, shippingDetails); 
       cart.Clear(); 
       return View("Completed"); 
      } 
      else 
      { 
       return View(shippingDetails); 
      } 
     } 

     public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) 
     { 
      Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId); 

      if (product != null) 
      { 
       cart.AddItem(product, 1); 
      } 

      return RedirectToAction("Index", new { returnUrl }); 
     } 

     public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl) 
     { 
      Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId); 

      if (product != null) 
      { 
       cart.RemoveLine(product); 
      } 

      return RedirectToAction("Index", new { returnUrl }); 
     } 

     public ViewResult Index(Cart cart, string returnUrl) 
     { 
      return View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl }); 
     } 


     public ViewResult Summary(Cart cart) 
     { 
      return View(cart); 
     } 

     [HttpGet] 
     public ViewResult Checkout() 
     { 
      return View(new ShippingDetails()); 
     } 

     private Cart GetCart() 
     { 
      Cart cart = (Cart)Session["Cart"]; 
      if (cart == null) 
      { 
       cart = new Cart(); 
       Session["Cart"] = cart; 
      } 
      return cart; 
     } 

    } 
} 

所示。但是,每當我按這個輸入按鈕,沒有任何反應。

有人可以告訴我什麼是錯的?我認爲提交類型的輸入按鈕會調用該操作的發佈版本,但顯然這不起作用。

編輯:

我嘗試禁用瀏覽器中所有的JavaScript,但這並不解決問題。

這裏是我的路由信息​​:

public static void RegisterRoutes(RouteCollection routes) 


{ 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(null,"", // Only matches the empty URL (i.e. /) 
      new 
      { 
      controller = "Product", 
      action = "List", 
      category = (string)null, 
      page = 1 
      } 
      ); 

     routes.MapRoute(null, "Page{page}", new { Controller = "Product", action = "List" }); 

     routes.MapRoute(null,"{category}", // Matches /Football or /AnythingWithNoSlash 
      new { controller = "Product", action = "List", page = 1 }); 

     routes.MapRoute(null, 
     "{category}/Page{page}", // Matches /Football/Page567 
     new { controller = "Product", action = "List" }, // Defaults 
     new { page = @"\d+" } // Constraints: page must be numerical 
     ); 

     routes.MapRoute(null, "{controller}/{action}"); 
    } 

這裏是在Global.asax中

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 
     RegisterRoutes(RouteTable.Routes); 

     ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); 
     ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder()); 
    } 

我真的不知道還能做什麼我ApplicationStart方法。如果有人有任何想法,請告訴我。

回答

1

我期望類型的輸入提交給從我的控制器調用POST版本結帳行動

你爲什麼想到這樣的事情?你似乎並沒有把它顯示在窗體上:

@using (Html.BeginForm("Checkout", "Cart")) 
{ 
    ... 
} 

如果沒有明確指定動作和控制器的名稱呈現形式,那麼相同的動作作爲渲染這一觀點的一個時調用將用HttpPost調用。因此,例如,如果此視圖是從Index操作呈現的,那麼如果您使用Html.BeginForm,則ASP.NET MVC將在同一控制器上查找帶有HttpPost的索引操作。

例如:

public ActionResult Index() 
{ 
    ... render the form 
} 

[HttpPost] 
public ActionResult Index(ShippingDetails shippingDetails, Cart cart) 
{ 
    ... process the form submission 
} 

這是慣例。如果你不想遵循約定,你需要使用Html.BeginForm的重載,它允許你指定你想要調用的動作和控制器。

+0

嗨,謝謝你的回答。我編輯了使用Html.BeginForm(「Checkout」,「Cart」)的代碼(並且也在我的問題中編輯了列表)。但是,我仍然沒有達到斷點,也沒有任何反應。你還有其他建議嗎?謝謝! – Locust5304

+0

那麼當你在'Html.BeginForm'中明確指定控制器和動作時會發生什麼?當您提交此表單時,它將被髮送到'Cart'控制器上的'Checkout'控制器動作。你有錯誤嗎? –

+0

我仍然沒有達到斷點,沒有任何反應。你還有其他建議嗎? – Locust5304

1

您必須明確指定您要POST表單。默認情況下,它執行GET。

@using(Html.BeginForm("Checkout", "Cart", FormMethod.Post)) 
{ 
    ... 
}