简体   繁体   English

我的 ASP.NET Core MVC 应用程序停止保存更改

[英]My ASP.NET Core MVC application stopped saving changes

My ASP.NET Core MVC application (written in C#) stopped for some reason to save changes in database.我的 ASP.NET 核心 MVC 应用程序(用 C# 编写)由于某种原因停止在数据库中保存更改。

It reads perfectly and there is no any error related to connection itself.它读起来很完美,没有任何与连接本身相关的错误。 It worked perfectly yesterday but today it is not longer working.它昨天工作得很好,但今天它不再工作了。 What is going on in here?这是怎么回事? Could it be related to a "refresh" problem.它可能与“刷新”问题有关。

This is my controller code:这是我的 controller 代码:

// POST: Approver/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, [Bind] Approver approver)
{
    try
    {
        if (ModelState.IsValid)
        {
            dbContext.UpdateApprover(approver);
            return RedirectToAction("Index");
        }

        return View(dbContext);
    }
    catch
    {
        return View();
    }
}

This is my CONTEXT update method:这是我的 CONTEXT 更新方法:

public void UpdateApprover(Approver approver)
{
     using (SqlConnection con = new SqlConnection(connectionString))
     {
         SqlCommand cmd = new SqlCommand("Stored_procedure_update", con)
            {
                CommandType = System.Data.CommandType.StoredProcedure
            };

         cmd.Parameters.AddWithValue("@CODE", approver.CODE);
         cmd.Parameters.AddWithValue("@MAIL", approver.MAIL);
         cmd.Parameters.AddWithValue("@NAME", approver.NAME);

         con.Open();
         cmd.ExecuteNonQuery();
         con.Close();
     }
}

Am I missing something?我错过了什么吗?


CONNECTION FILE:连接文件:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyApplication.Models;

namespace MyApplication.Context
{
    public class Connection
    {
        string connectionString = "Data Source=xxxxxxx;Initial Catalog = ''; Persist Security Info=True;User ID = ''; Password='xxxxxx'";

        /// <summary>
        /// </summary>
        /// <returns></returns>

        public IEnumerable<Approver> GetAllApprovers()
        {
            var ApproversList = new List<Approver>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    var Approver = new Approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString(),
                        FIRSTNAME = dr["FIRSTNAME"].ToString(),
                        LASTNAME = dr["LASTNAME"].ToString()
                    };

                    ApproversList.Add(Approver);
                }
                con.Close();
            }

            return ApproversList;

        }

        public void UpdateApprover(Approver Approver)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_Update_Approver", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@CODE", Approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", Approver.MAIL);
                cmd.Parameters.AddWithValue("@FIRSTNAME", Approver.FIRSTNAME);
                cmd.Parameters.AddWithValue("@LASTNAME", Approver.LASTNAME);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }


        }

        public Approver GetApproverById(int? id)
        {
            var Approver = new Approver();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", id);

                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    Approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
                    Approver.MAIL = dr["MAIL"].ToString();
                    Approver.FIRSTNAME = dr["FIRSTNAME"].ToString();
                    Approver.LASTNAME = dr["LASTNAME"].ToString();
                }

                con.Close();
            }

            return Approver;

        }


    }
}

CONTROLLER FILE: CONTROLLER 文件:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyApplication.Context;
using MyApplication.Models;

namespace MyApplication.Controllers
{
    public class ControllerApprover : Controller
    {
        readonly Connection dbContext = new Connection();

        // GET: Approver
        public ActionResult Index()
        {
            List<Approver> ApproversList = dbContext.GetAllApprovers().ToList();
            return View(ApproversList);
        }

        // GET: Approver/Details/5
        public ActionResult Details(int id)
        {
            Approver approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // GET: Approver/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Approver/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(Approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

        // GET: Approver/Edit/5
        public ActionResult Edit(int id)
        {
            Approver Approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // POST: Approver/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

    }
}

My appsettings.json:我的 appsettings.json:

  {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"}

This should be in the comment section, but I can't comment right now, need 50 reputation.这应该在评论部分,但我现在不能评论,需要 50 声望。 But I think, you could try first see if there is an error on your catch block, change it to catch (Exception e) to see if there is something there.但我认为,你可以先试试你的 catch 块是否有错误,将其更改为catch (Exception e)以查看是否有问题。 Log it, or put a breakpoint in debugging mode.记录它,或者在调试模式下放置一个断点。

Correctly pick your connection string and change your CONTEXT method to something like that, if still not work.如果仍然无法正常工作,请正确选择您的连接字符串并将您的 CONTEXT 方法更改为类似的方法。

private IConfiguration Configuration;
public HomeController(IConfiguration _configuration)
    {
        Configuration = _configuration;
    }





public void UpdateApprover(Approver approver)
{

  string connectionString=  Configuration.GetConnectionString("YOUR CONNECTION STRING NAME");
     using (SqlConnection con = new SqlConnection(connectionString))
     {
       con.Open();
        using (SqlCommand cmd = new SqlCommand("Stored_procedure_update", con))
            {
                CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", approver.MAIL);
                cmd.Parameters.AddWithValue("@NAME", approver.NAME);

         
         cmd.ExecuteNonQuery();
         con.Close();
            }
     }
}


CONNECTION FILE:连接文件:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyApplication.Models;

namespace MyApplication.Context
{
    public class Connection
    {
        string connectionString = "Data Source=xxxxxxx;Initial Catalog = ''; Persist Security Info=True;User ID = ''; Password='xxxxxx'";

        /// <summary>
        /// </summary>
        /// <returns></returns>

        public IEnumerable<Approver> GetAllApprovers()
        {
            var ApproversList = new List<Approver>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    var Approver = new Approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString(),
                        FIRSTNAME = dr["FIRSTNAME"].ToString(),
                        LASTNAME = dr["LASTNAME"].ToString()
                    };

                    ApproversList.Add(Approver);
                }
                con.Close();
            }

            return ApproversList;

        }

        public void UpdateApprover(Approver Approver)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_Update_Approver", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@CODE", Approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", Approver.MAIL);
                cmd.Parameters.AddWithValue("@FIRSTNAME", Approver.FIRSTNAME);
                cmd.Parameters.AddWithValue("@LASTNAME", Approver.LASTNAME);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }


        }

        public Approver GetApproverById(int? id)
        {
            var Approver = new Approver();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", id);

                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    Approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
                    Approver.MAIL = dr["MAIL"].ToString();
                    Approver.FIRSTNAME = dr["FIRSTNAME"].ToString();
                    Approver.LASTNAME = dr["LASTNAME"].ToString();
                }

                con.Close();
            }

            return Approver;

        }


    }
}

CONTROLLER FILE: CONTROLLER 文件:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyApplication.Context;
using MyApplication.Models;

namespace MyApplication.Controllers
{
    public class ControllerApprover : Controller
    {
        readonly Connection dbContext = new Connection();

        // GET: Approver
        public ActionResult Index()
        {
            List<Approver> ApproversList = dbContext.GetAllApprovers().ToList();
            return View(ApproversList);
        }

        // GET: Approver/Details/5
        public ActionResult Details(int id)
        {
            Approver approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // GET: Approver/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Approver/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(Approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

        // GET: Approver/Edit/5
        public ActionResult Edit(int id)
        {
            Approver Approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // POST: Approver/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

    }
}

My appsettings.json:我的 appsettings.json:

  {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"}

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

相关问题 ASP.NET Core MVC应用程序洞察在升级到.NET Core 1.1.1后停止工作 - ASP.NET Core MVC application insights stopped working after upgrade to .NET Core 1.1.1 为什么我的文件没有保存到我的 ASP.NET 核心应用程序中的指定目录? - Why are my files not saving to my specified directory in my ASP.NET Core Application? ASP.NET MVC5应用程序中的LinkedIn OAuth停止工作 - LinkedIn oauth in asp.net mvc5 application stopped working C#ASP.NET MVC代码首先不保存更改 - C# ASP.NET MVC Code First Not Saving Changes 我的帖子表单没有达到我的 asp.net mvc 核心 web 应用程序中的操作方法 - My Post form is not reaching the action method inside my asp.net mvc core web application 将ASP.NET Core MVC应用程序集成到现有的ASP.NET MVC应用程序中 - Integrate ASP.NET Core MVC Application to existing ASP.NET MVC Application 如何防止保存文件ASP.NET MVC 4应用程序 - How to prevent saving files asp.net mvc 4 application 在我的 asp.net 核心 MVC web 应用程序中,我如何使验证码成为必填字段 - Inside my asp.net core MVC web application how i can make the recaptcha a required field 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? 我的 ASP.NET Core 按钮根本不发送任何内容(ASP.NET Core MVC 和 C#) - My ASP.NET Core button not sending anything at all (ASP.NET Core MVC and C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM