pmichna
pmichna

Reputation: 4888

MVC 4 - HttpPost is not fired

My CreateController.cs:

using System.Web.Mvc;
using ClientDAL;
using Services;
using ShardingMvcDemo.Raven;
using ShardingMvcDemo.ViewModels;

namespace ShardingMvcDemo.Controllers
{
    public class CreateController : Controller
    {
        //
        // GET: /Create/

        public ActionResult Index()
        {
            return View();
        }


        //
        // POST: /Create/

        [HttpPost]
        public ActionResult Create(ClientViewModel client)
        {
            try
            {
                var ravenDbConnection = new RavenDbConnection(new RavenDbConnectionManager());
                var service = new ClientService(ravenDbConnection);
                service.AddClient(client.FirstName, client.LastName, client.Country);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

My Create/Index.cshtml view:

@model ShardingMvcDemo.ViewModels.ClientViewModel

@{
    ViewBag.Title = "Create a client";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Client</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Country)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Country)
            @Html.ValidationMessageFor(model => model.Country)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

When I click the submitting button in the form, the [HttpPost] method is not even triggered (checked in debug mode). What am I doing wrong?

Upvotes: 1

Views: 6138

Answers (2)

Juli&#235;n
Juli&#235;n

Reputation: 9522

Perhaps you should try to explicitely define the controller and action in your form's initialization, like so:

@using (Html.BeginForm("Create", "Create")) { ...

In your current setup you're trying to POST to /create/index, instead of /create/create.

Upvotes: 1

gzaxx
gzaxx

Reputation: 17590

It doesn't match your view name. Change it to:

public ActionResult Index(ClientViewModel client)

Upvotes: 4

Related Questions