0

My create button isn't working when clicked on. Can someone please tell me where I went wrong?

I tried a few forums but they haven't solved the issue.

Controller:

using Rentals.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Rentals.Controllers
{
    public class GenreController : Controller
    {
        private ApplicationDbContext db;

        public GenreController()
        {
            db = new ApplicationDbContext();
        }

        // GET: Genre
        public ActionResult Index()
        {
            return View(db.Genres.ToList());
        }

        //getaction
        public ActionResult Create()
        {
            return View();
        }

        //post action to insert data into database
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Genre genre)
        {
            if (ModelState.IsValid)
            {
                db.Genres.Add(genre);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
        }
    }
}

View (create.cshtml):

@model Rentals.Models.Genre
@{
    ViewBag.Title = "Create";
}

@using (Html.BeginForm())

{
    @Html.AntiForgeryToken()
}

<div class="form-horizontal">
    <h3> Create new Genre</h3>
    <hr />

    <div class="form-group">
        @Html.LabelFor(m=>m.Name, htmlAttributes: new { @class ="control-label col-md-2"})
        <div class="col-md-10">
         @Html.EditorFor(m=>m.Name, new {htmlAttributes = new {@class="form-control"}})
            @Html.ValidationMessageFor(m=>m.Name,"", new { @class = "text-danger"})
        </div>
    </div>
</div>
<div>
    <input type="submit" value="Create" class="btn btn-sm btn-success" />
    @Html.Partial("_BackToListPartial")
</div>

When I click on the create button it should add to the database. For some reason when I click it, it's not doing anything.

1
  • 2
    keep your html form in from action. Please refer youtube for basic mvc crud operations and html binding with actions. Commented Jul 5, 2019 at 18:25

1 Answer 1

2

Wrap the form around your HTML.

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


<div class="form-horizontal">
    <h3> Create new Genre</h3>
    <hr />

    <div class="form-group">
        @Html.LabelFor(m=>m.Name, htmlAttributes: new { @class ="control-label col-md-2"})
        <div class="col-md-10">
         @Html.EditorFor(m=>m.Name, new {htmlAttributes = new {@class="form-control"}})
            @Html.ValidationMessageFor(m=>m.Name,"", new { @class = "text-danger"})
        </div>
    </div>
</div>
<div>
    <input type="submit" value="Create" class="btn btn-sm btn-success" />
    @Html.Partial("_BackToListPartial")
</div>
}
Sign up to request clarification or add additional context in comments.

2 Comments

@RahulSharma if you don't define the action and controller, it just does a post request to the same action. Which is how it's set up in the controller. I agree that most of the time it is good to specify the action and controller though.
@KevinLamb Yes, I did not see that View name is same as Controller action method. This will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.