Question

I have a strange situation. I use the BeginForm helper in ASP.NET MVC. Normally, if you use a viewmodel on a view, then it will post to the viewmodel to my controller.

Right now it will hit my HTTPGET method, which has no parameters. My best guess is the viewmodel isn't being sent back to the server.

I have attached my view, controller and viewmodel code below.

How do I make sure my HttpPost method is being hit, when submitting the form? Am I doing something obvious wrong?

My view:

@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>

My controller:

 [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
        }

My viewmodel:

 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;
        }
    }
Was it helpful?

Solution

This was stupid. I decided not to delete the question, because if anyone actually seeks this on Google, this might help.

For search engine optimization reasons, I had the following entry in my web.config:

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

That would obviously screw up these things.

So if you find this on google: make sure you don't have any strange rules and routes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top