Html.BeginForm non sta postando sul server nonostante il pulsante di invio

Ho un modulo standard, che è racchiuso in un Html.BeginForm. Nella parte inferiore c’è un pulsante di invio.

Tuttavia, quando si fa clic sul pulsante il mio controller non viene colpito. Sperimento un ricarico della pagina, ma nessun codice viene colpito. Ho provato a eseguire il debug sul lato controller e provare il codice che potrei vedere essere colpito, ma sicuramente non viene colpito.

Rasoio – Index.cshtml in /Views/BuyBitcoins/Index.cshtml:

@using (Html.BeginForm("BuyBitcoinsNow", "BuyBitcoins",FormMethod.Post)) { 

@ModelRes.BuyBitcoinStrings.Form_YourPurchase

@ModelRes.BuyBitcoinStrings.Form_HowMuch
@Html.EditorFor(model => model.PurchaseViewModel.PurchaseAmount)

@ModelRes.BuyBitcoinStrings.Form_Summary

@ModelRes.BuyBitcoinStrings.Form_Personal_Headline

@ModelRes.BuyBitcoinStrings.Form_Name:
@Html.EditorFor(model => model.Name)

@ModelRes.BuyBitcoinStrings.Form_Email:
@Html.EditorFor(model => model.Email)

@ModelRes.BuyBitcoinStrings.Form_Phone
@Html.EditorFor(model => model.Phone)

  • @ModelRes.BuyBitcoinStrings.Form_usp1
  • @ModelRes.BuyBitcoinStrings.Form_usp2
  • @ModelRes.BuyBitcoinStrings.Form_usp3
}

RouteConfig.cs:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Order info", url: "orders/{id}", defaults: new { controller = "Orders", action = "Index", id = "" }); routes.MapRoute( name: "Shop", url: "shop/{action}/{id}", defaults: new { controller = "Shop", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); foreach (Route r in routes) { if (!(r.RouteHandler is SingleCultureMvcRouteHandler)) { r.RouteHandler = new MultiCultureMvcRouteHandler(); r.Url = "{culture}/" + r.Url; if (r.Defaults == null) { r.Defaults = new RouteValueDictionary(); } r.Defaults.Add("culture", Culture.en.ToString()); //Adding constraint for culture param if (r.Constraints == null) { r.Constraints = new RouteValueDictionary(); } r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(), Culture.da.ToString())); } } } 

Il mio controller, BuyBitcoinsController.cs :

 public OrderManager OrderManager { get; set; } public BuyBitcoinsController() { OrderManager = new OrderManager(); } public ActionResult Index() { return View(new BuyBitcoinViewModel()); } [HttpPost] public ActionResult BuyBitcoinsNow(BuyBitcoinViewModel model) { OrderManager.CreateOrder(new Order() { BitcoinAddress = model.PurchaseViewModel.BitcoinAddress, DateCreated = DateTime.Now, OrderState = OrderState.OrderCreated, Guid = Guid.NewGuid(), Logs = new Dictionary(), User = new XbtUser() { Email = model.Email, FirstName = model.Name, Phone = model.Phone, IsAnonymous = true, } }); return View("Index"); // return View("~/Views/Orders/25892e17-80f6-415f-9c65-7395632f0223"); } 

Gli appunti

Ho provato a commentare le rotte della lingua in RouteConfig.cs (il ciclo foreach), ma non ha ancora effettuato alcun postback sul mio controller).

Inoltre, non ci sono errori javascript sul caricamento o clic secondo FireBug.

Usa Index invece di BuyBitcoinsNow per il nome dell’azione. Ho testato il tuo codice, tranne che non ho usato ModelRes , funziona per me. Vedi il mio esempio:

Azione

 public ActionResult Index() { return View(new BuyBitcoinViewModel()); } [HttpPost] public ActionResult Index(BuyBitcoinViewModel model) { // return View("Index"); } 

vista

 @model Demo.Models.BuyBitcoinViewModel @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { 

@Html.EditorFor(model => model.PurchaseViewModel.PurchaseAmount)

@Html.EditorFor(model => model.PurchaseViewModel.BitcoinAddress)

@Html.EditorFor(model => model.Name)

@Html.EditorFor(model => model.Email)

@Html.EditorFor(model => model.Phone)

}

Modello

  public class BuyBitcoinViewModel { public PurchaseViewModel PurchaseViewModel { get; set; } public string Name { get; set; } public string Email { get; set; } public string Phone { get; set; } } public class PurchaseViewModel { public string PurchaseAmount { get; set; } public string BitcoinAddress { get; set; } } 

RouteConfig.cs

 public class RouteConfig { public enum Culture { ru = 1, en = 2, da =3 } public class CultureConstraint : IRouteConstraint { private string[] _values; public CultureConstraint(params string[] values) { this._values = values; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { string value = values[parameterName].ToString(); return _values.Contains(value); } } public class MultiCultureMvcRouteHandler : MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { var culture = requestContext.RouteData.Values["culture"].ToString(); var ci = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = ci; Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name); return base.GetHttpHandler(requestContext); } } public class SingleCultureMvcRouteHandler : MvcRouteHandler { } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Order info", url: "orders/{id}", defaults: new { controller = "Order", action = "Index", id = "" }); routes.MapRoute( name: "Shop", url: "shop/{action}/{id}", defaults: new {controller = "Shop", action = "Index", id = UrlParameter.Optional} ); foreach (Route r in routes) { if (!(r.RouteHandler is SingleCultureMvcRouteHandler)) { r.RouteHandler = new MultiCultureMvcRouteHandler(); r.Url = "{culture}/" + r.Url; if (r.Defaults == null) { r.Defaults = new RouteValueDictionary(); } r.Defaults.Add("culture", Culture.en.ToString()); if (r.Constraints == null) { r.Constraints = new RouteValueDictionary(); } r.Constraints.Add("culture", new CultureConstraint(Culture.en.ToString(),Culture.da.ToString())); } } } }