0

我已经创建了在 web.config 文件中更新 sql 连接字符串的功能。但是,由于某种原因,我无法在不刷新页面的情况下实时更新它。刷新页面后,它会显示更改的值。当我单击保存时它保存在 web.config 文件中,但返回到旧值。

这是我的管理员模型:

   public void SAVEsqlConnection(string sqlConnection)
    {
        if (sqlConnection != System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnection1"].ToString())
        {
            Configuration sqlConnection1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            var section = (ConnectionStringsSection)sqlConnection1.GetSection("connectionStrings");
            section.ConnectionStrings["sqlConnection1"].ConnectionString = sqlConnection;
            sqlConnection1.Save();
        }
    }

这是我的管理员控制器:

    [HttpPost]
    public ActionResult Admin(string txt_file_dest, string report_dest, string sql_Connection)
    {
        AdminModel Values = new AdminModel();

        if (sql_Connection != null)
        {
            Values.SAVEsqlConnection(sql_Connection);       
        }

        return View();
    }

这是我的 Admin.cshtml 文件:

    <input type="text" name="sql_Connection" value= "@System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnection1"]">
    :SQL Connection String<br>
    <br>
    <input type="submit" name="Save" value="Save Changes" />

任何帮助,将不胜感激。我有一个最后期限。

提前致谢。

4

2 回答 2

0

You can refresh the page using this bit of JavaScript (1.2) after the query has been done:

 <script>
 window.location.reload(true); 
 </script>

Althought, AJAX would be a better solution because it would make the page autoload new content.

于 2013-04-01T18:21:09.047 回答
0
[HttpPost]
    public ActionResult Admin(string txt_file_dest, string report_dest, string sql_Connection)
    {
        AdminModel Values = new AdminModel();

        if (sql_Connection != null)
        {
            Values.SAVEsqlConnection(sql_Connection);       
        }

         return RedirectToAction("actionname");//call same page action again
    }
于 2013-04-02T04:24:42.683 回答