C#ASP.NET MVC创建按钮在单击时不起作用

C#ASP.NET MVC创建按钮在单击时不起作用,c#,model-view-controller,C#,Model View 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 GenreCont

单击“创建”按钮时,我的“创建”按钮不起作用。有人能告诉我哪里出了问题吗

我尝试了几个论坛,但都没有解决这个问题

控制器:

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();
        }
    }
}
视图(create.cshtml):

@model Rentals.Models.Genre
@{
ViewBag.Title=“创建”;
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
}
创造新的体裁

@LabelFor(m=>m.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(m=>m.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Name,“,new{@class=“text danger”}) @Html.Partial(“\u BackToListPartial”)

当我单击create按钮时,它应该添加到数据库中。由于某种原因,当我单击它时,它没有做任何事情。

将表单包装在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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
创造新的体裁

@LabelFor(m=>m.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(m=>m.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Name,“,new{@class=“text danger”}) @Html.Partial(“\u BackToListPartial”) }
将表单包装在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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
创造新的体裁

@LabelFor(m=>m.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(m=>m.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(m=>m.Name,“,new{@class=“text danger”}) @Html.Partial(“\u BackToListPartial”) }
保持html表单不动。请参考youtube了解基本的mvc crud操作和带有操作的html绑定。保持html表单不动。请参考youtube了解基本的mvc crud操作和带有操作的html绑定。@RahulSharma如果不定义操作和控制器,它只对同一操作执行post请求。这就是它在控制器中的设置方式。我同意大多数时候指定操作和控制器是好的。@KevinLamb是的,我没有看到
View
名称与
controller
操作方法相同。这将起作用。@RahulSharma如果不定义动作和控制器,它只对同一动作执行post请求。这就是它在控制器中的设置方式。我同意大多数时候指定操作和控制器是好的。@KevinLamb是的,我没有看到
View
名称与
controller
操作方法相同。这会奏效的。