私には奇妙な状況があります。BeginForm
ASP.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;
}
}