I have a standard form, which is wrapped into a Html.BeginForm. In the bottom there is a submit button.
However, when clicking the button my controller isn't getting hit. I experience a reload of the page, but no code is hit. I've tried to debug on the controllers side and try code I could see get hit, but it's definetely not being hit.
Razor - Index.cshtml under /Views/BuyBitcoins/Index.cshtml:
@using (Html.BeginForm("BuyBitcoinsNow", "BuyBitcoins",FormMethod.Post))
{
<h3>@ModelRes.BuyBitcoinStrings.Form_YourPurchase</h3>
<p>
@ModelRes.BuyBitcoinStrings.Form_HowMuch<br />
@Html.EditorFor(model => model.PurchaseViewModel.PurchaseAmount)
</p>
<div id="plcBitcoinAddress">
<p>
@ModelRes.BuyBitcoinStrings.Form_EnterAddress<br />
@Html.EditorFor(model => model.PurchaseViewModel.BitcoinAddress)
</p>
</div>
<h3>@ModelRes.BuyBitcoinStrings.Form_Summary</h3>
<h3>@ModelRes.BuyBitcoinStrings.Form_Personal_Headline</h3>
<p>
@ModelRes.BuyBitcoinStrings.Form_Name:<br />
@Html.EditorFor(model => model.Name)
</p>
<p>
@ModelRes.BuyBitcoinStrings.Form_Email:<br />
@Html.EditorFor(model => model.Email)
</p>
<p>
@ModelRes.BuyBitcoinStrings.Form_Phone<br />
@Html.EditorFor(model => model.Phone)
</p>
<div id="plcTerms" class="checkbox">
<label for="termsChk">
<input id="termsChk" type="checkbox">
@ModelRes.BuyBitcoinStrings.Form_accept
</label>
</div>
<div id="newsletterTerms" class="checkbox">
<label for="newsletterChk">
<input id="newsletterChk" type="checkbox" checked="checked">
@ModelRes.BuyBitcoinStrings.Form_newsletter
</label>
</div>
<div id="usp_list">
<ul>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
@ModelRes.BuyBitcoinStrings.Form_usp1</li>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
@ModelRes.BuyBitcoinStrings.Form_usp2</li>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
@ModelRes.BuyBitcoinStrings.Form_usp3</li>
</ul>
</div>
<input id="submitBtn" type="submit" value="@ModelRes.BuyBitcoinStrings.Form_button" class="btn btn-success btn-lg" />
}
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()));
}
}
}
My 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<DateTime, OrderSystemLog>(),
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");
}
Notes
I have tried to comment out the language routes in RouteConfig.cs (the foreach loop), but it still made no postback to my controller).
Also, there are no javascript errors on load or click according to FireBug.
Use Index
instead of BuyBitcoinsNow
for the action name. I tested your code, except I didn't use ModelRes
, it works for me. See my example:
Action
public ActionResult Index()
{
return View(new BuyBitcoinViewModel());
}
[HttpPost]
public ActionResult Index(BuyBitcoinViewModel model)
{
//
return View("Index");
}
View
@model Demo.Models.BuyBitcoinViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<p>
@Html.EditorFor(model => model.PurchaseViewModel.PurchaseAmount)
</p>
<div id="plcBitcoinAddress">
<p>
@Html.EditorFor(model => model.PurchaseViewModel.BitcoinAddress)
</p>
</div>
<p>
@Html.EditorFor(model => model.Name)
</p>
<p>
@Html.EditorFor(model => model.Email)
</p>
<p>
@Html.EditorFor(model => model.Phone)
</p>
<div id="plcTerms" class="checkbox">
<label for="termsChk">
<input id="termsChk" type="checkbox">
</label>
</div>
<div id="newsletterTerms" class="checkbox">
<label for="newsletterChk">
<input id="newsletterChk" type="checkbox" checked="checked">
</label>
</div>
<div id="usp_list">
<ul>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
</li>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
</li>
<li>
<img src="@Url.Content("~/Content/images/1387430978_checkmark-g.png")" style="width: 20px;" />
</li>
</ul>
</div>
<input id="submitBtn" type="submit" value="Submit" class="btn btn-success btn-lg" />
}
Model
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()));
}
}
}
}