Html.BeginForm не публикуется на сервере, несмотря на кнопку отправки
У меня есть стандартная форма, которая заключена в Html.BeginForm. Внизу есть кнопка отправки.
Однако при нажатии на кнопку мой контроллер не получает удар. Я испытываю перезагрузку страницы, но код не ударил. Я пытался отлаживать на стороне контроллеров и пробовать код, который я видел, чтобы получить удар, но он определенно не был получен.
Razor - Index.cshtml в /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()));
}
}
}
Мой контроллер 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");
}
Заметки
Я попытался закомментировать языковые маршруты в RouteConfig.cs (цикл foreach), но он все еще не сделал обратной передачи на мой контроллер).
Кроме того, нет никаких ошибок JavaScript при загрузке или щелчке в соответствии с FireBug.
1 ответ
Использование Index
вместо BuyBitcoinsNow
для имени действия. Я проверил ваш код, за исключением того, что я не использовал ModelRes
, меня устраивает. Смотрите мой пример:
действие
public ActionResult Index()
{
return View(new BuyBitcoinViewModel());
}
[HttpPost]
public ActionResult Index(BuyBitcoinViewModel model)
{
//
return View("Index");
}
Посмотреть
@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" />
}
модель
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()));
}
}
}
}