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 ofBuyBitcoinsNow
for the action name. I tested your code, except I didn't useModelRes
, it works for me. See my example:Action
View
Model
RouteConfig.cs