db.className.Update(model);不起作用,并且不更新数据库

时间:2019-07-10 21:56:22

标签: sql asp.net sql-server database asp.net-core

我尝试更新数据库,但是它不起作用。我有一个操作来添加和编辑数据库行。添加有效,但更新无效。我使用了db.update()db.saveChanges编辑数据库行,但是它不起作用。我将在下面显示我的模型,视图和控制器。对不起,如果我是一个业余爱好者。请告诉我该怎么办。谢谢大家。

这是我的模特

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace WarOfWords.Models
{
    public class storyMode
    {
        [Key]
        public int ID { get; set; }
        [Required]
        public bool isOpen { get; set; }
        [Required]
        public String StoryImage { get; set; }
        [Required]
        public String StroyName { get; set; }
        [Required]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public String StoryDecription { get; set; }
    }
}

这是我的表格:

@model WarOfWords.Models.storyMode
@using WarOfWords.PublicModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, WarOfWords
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_panel.cshtml";
}
@Html.AntiForgeryToken()
<form asp-controller="story" asp-action="AddEditStory" method="post" role="form">
    <div class="modal-body form-horizontal">
        <div class="row">
            <div class="form-group myFormGroup hidden">
                <label asp-for="ID" class="col-lg-3 col-sm-3 control-label formLabel"></label>
                <div class="col-lg-6 myInputContainer">
                    <input asp-for="ID" class="form-control formInput" />
                </div>
            </div>
            <div class="form-group myFormGroup">
                <label asp-for="StroyName" class="col-lg-3 col-sm-3 control-label formLabel"></label>
                <div class="col-lg-6 myInputContainer">
                    <input asp-for="StroyName" class="form-control formInput" />
                </div>
            </div>

            <div class="form-group myFormGroup">
                <label asp-for="isOpen" class="col-lg-3 col-sm-3 control-label formLabel"></label>
                <div class="col-lg-6 myInputContainer">
                    <input asp-for="isOpen" class="form-control formInput" />
                </div>
            </div>

            <div class="form-group myFormGroup">
                <label asp-for="StoryImage" class="col-lg-3 col-sm-3 control-label formLabel"></label>
                <div class="col-lg-6 myInputContainer">
                    <input type="file" asp-for="StoryImage" class="form-control formInput" />
                </div>
            </div>

            <div class="form-group myFormGroup">
                <label asp-for="StoryDecription" class="col-lg-3 col-sm-3 control-label formLabel"></label>
                <div class="col-lg-6 myInputContainer">
                    <input asp-for="StoryDecription" class="form-control formInput" />
                    @*<textarea id="editor1" name="editor1" asp-for="StoryDecription" class="form-control  "></textarea>*@
                </div>
            </div>
        </div>
    </div>
</form>

这是我在控制器中的动作:

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddEditStory(storyMode model, int ID)
{
    if (ModelState.IsValid)
    {
        //if true   
        if (ID == 0)
        {
            //insert
            using (var db = _serviceProvider.GetRequiredService<ApplicationDbContext>())
            {
                db.storymodes.Add(model);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        else
        {
            //update
            using (var db = _serviceProvider.GetRequiredService<ApplicationDbContext>())
            {
                db.storymodes.Update(model);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
    }
    else
    {
        return PartialView("_AddEditStory", model);
    }
}

0 个答案:

没有答案