2

私には奇妙な状況があります。BeginFormASP.NET MVC でヘルパーを使用します。通常、ビューでビューモデルを使用すると、ビューモデルがコントローラーにポストされます。

現時点では、パラメーターを持たない HTTPGET メソッドにヒットします。私の最善の推測は、ビューモデルがサーバーに送り返されていないことです。

ビュー、コントローラー、ビューモデルのコードを以下に添付しました。

フォームを送信するときに、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;
        }
    }
4

1 に答える 1

1

これはばかげていました。誰かが実際にGoogleでこれを探しているなら、これが役立つかもしれないので、私は質問を削除しないことに決めました.

検索エンジンの最適化の理由から、web.config に次のエントリがありました。

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

それは明らかにこれらのものを台無しにするでしょう。

したがって、Google でこれを見つけた場合: 変なルールやルートがないことを確認してください。

于 2014-03-15T22:29:12.710 に答える