繁体   English   中英

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

[英]C# ASP.NET MVC Create Button is not working on click

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

我尝试了几个论坛,但他们还没有解决问题。

控制器:

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 = "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>

当我单击创建按钮时,它应该添加到数据库中。 由于某种原因,当我单击它时,它什么也没做。

将表单包装在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>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM