Code_Viking
Code_Viking

Reputation: 628

Input from mvc application is not getting submitted in database

I have created a simple MVC application. It has a view called Index in the folder home, in the view i have some input boxes, wich inputs i wish to save as a object and save in a database. i have create the view from a controller that i have created. My problem is when i click the button for submit nothing seems to happen?

Here is the code i have in the view.

    @model FlexMVC.FlexBookingDb.Resevation

@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

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

    <div class="form-horizontal">
        <h4>Resevation</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ResevationDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ResevationDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ResevationDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ResevationTime, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ResevationTime, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ResevationTime, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Car, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Car, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Car, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Customer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Customer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Customer, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StartLocation, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartLocation, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StartLocation, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EndLocation, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EndLocation, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EndLocation, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

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

And here is my controller code.

namespace FlexMVC.Controllers
{
    public class ResevationController : Controller
    {
        // GET: Resevation
        public ActionResult Index()
        {
            return View();
        }

        // POST: Resevation
        [HttpPost]
        public ActionResult Create(Resevation resevation)
        {
            ResevationModels model = new ResevationModels();

            resevation.Car = 1;
            resevation.Customer = 1;

            model.CreateResevation(resevation);

            return View(model);
        }

    }
}

can anybody explain to me what i am doing wrong? and please say if i should post the model code as well.

Edit:
Here is the code for the CreateResevation Method.

public void CreateResevation(Resevation resevation)
    {
        resevation.Car = FindAvaliableCar(resevation.ResevationDate, resevation.ResevationTime);

        _db.Resevations.InsertOnSubmit(resevation);
        _db.SubmitChanges();
    }

Upvotes: 1

Views: 51

Answers (1)

Orel Eraki
Orel Eraki

Reputation: 12196

You have 2 problems.

  1. You probably submitting to the wrong action. You're submitting to Index while you probably meant to submit Create. This can be fixed by changing to @using (Html.BeginForm("Create", "ControllerNameHere"))

  2. The AntiForgeryToken needs a [ValidateAntiForgeryToken()] validation attribute on the proposed Action.

Change it to

[HttpPost]
[ValidateAntiForgeryToken()]
public ActionResult Create(Resevation resevation)

Upvotes: 2

Related Questions