尽管提交按钮,Html.BeginForm仍未发布到服务器

时间:2013-12-23 17:16:00

标签: c# asp.net asp.net-mvc razor html.beginform

我有一个标准表单,它包含在Html.BeginForm中。在底部有一个提交按钮。

然而,当点击按钮时,我的控制器没有被击中。我遇到了页面的重新加载,但没有遇到任何代码。我试图在控制器端进行调试并尝试我能看到的代码被击中,但它确实没有被击中。

Razor - /Views/BuyBitcoins/Index.cshtml下的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循环)中注释掉语言路由,但它仍然没有回发到我的控制器。)

此外,根据FireBug,加载或点击时没有javascript错误。

1 个答案:

答案 0 :(得分:6)

使用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()));
                }
            }

        }
    }