Asp.net MVC は初めてで、次のコードに問題があります。
@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 version Checkout アクションを呼び出すことを期待しています。
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 の ApplicationStart メソッドがあります
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
ModelBinders.Binders.Add(typeof(Cart), new CartModelBinder());
}
他に何をすべきか本当にわかりません。誰かアイデアがあれば教えてください。