Beginform没有发送视图模型,也没有到达我的httppost方法

时间:2014-03-15 22:09:59

标签: asp.net asp.net-mvc asp.net-mvc-4 http-post html.beginform

我有一个奇怪的情况。我在ASP.NET MVC中使用BeginForm帮助器。通常,如果您在视图上使用视图模型,那么它将发布到视图模型到我的控制器。

现在它将触及我的HTTPGET方法,该方法没有参数。我最好的猜测是viewmodel没有被发送回服务器。

我已在下面添加了我的视图,控制器和视图模型代码。

在提交表单时,如何确保我的HttpPost方法被击中?我做了一件明显不对的事吗?

我的观点

@using LetterAmazer.Websites.Client.Extensions

@model LetterAmazer.Websites.Client.ViewModels.CreditsViewModel

<h1>Buy credits</h1>

You currently have @Model.Credit.ToFriendlyMoney() credits.


<h3>How many credits do you want to purchase?</h3>
@using (Html.BeginForm("Credits", "User", FormMethod.Post))
{

    @Html.EditorFor(model => model.PurchaseAmount) <text>($)</text>

    <h3>Select payment method</h3>
    @Html.DropDownListFor(model => model.SelectedPaymentMethod, Model.PaymentMethods, new { @class = "form-control" })

    <input type="submit" class="btn btn-primary btn-lg" value="Purchase credits" />

}

<script type="text/javascript">
    $(document).ready(function () {
        $('#@Html.IdFor(model=>model.PurchaseAmount)').change(function () {
            var value = $('#@Html.IdFor(model => model.PurchaseAmount)').val();
            if (value <= 1) {
                $('#@Html.IdFor(model=>model.PurchaseAmount)').val('1');
            }
        });
    });
</script>

我的控制器:

 [HttpGet]
        public ActionResult Credits()
        {
            CreditsViewModel model = new CreditsViewModel();
            model.Credit = SessionHelper.Customer.Credit;
            model.CreditLimit = SessionHelper.Customer.CreditLimit;

            var possiblePaymentMethods =
                paymentService.GetPaymentMethodsBySpecification(new PaymentMethodSpecification()
                {
                    CustomerId = SessionHelper.Customer.Id
                });
            foreach (var possiblePaymentMethod in possiblePaymentMethods)
            {
                model.PaymentMethods.Add(new SelectListItem()
                {
                    Text = possiblePaymentMethod.Name,
                    Value = possiblePaymentMethod.Id.ToString()
                });
            }


            return View(model);
        }

        [HttpPost]
        public ActionResult Credits(CreditsViewModel model)
        {
            // stufff
        }

我的观看模式:

 public class CreditsViewModel
    {
        public List<SelectListItem> PaymentMethods { get; set; }
        public string SelectedPaymentMethod { get; set; }

        public int PurchaseAmount { get; set; }

        public decimal Credit { get; set; }
        public decimal CreditLimit { get; set; }

        public CreditsViewModel()
        {
            this.PaymentMethods = new List<SelectListItem>();
            this.PurchaseAmount = 50;
        }
    }

1 个答案:

答案 0 :(得分:1)

这是愚蠢的。我决定不删除这个问题,因为如果有人真的在Google上搜索这个问题,这可能有所帮助。

出于搜索引擎优化的原因,我在web.config中输入了以下条目:

    <rule name="LowerCaseRule1" stopProcessing="true">
      <match url="[A-Z]" ignoreCase="false"/>
      <action type="Redirect" url="{ToLower:{URL}}"/>
    </rule>

这显然搞砸了这些事情。

因此,如果您在谷歌上发现这一点:请确保您没有任何奇怪的规则和路线。