none
请问如何在controller中完成了一个写表后,在页面给出一个完成的提示? RRS feed

答案

  • 你好,

    据我所知,如果你使用ADO.NET或者EF执行插入操作后,会返回一个int的值。

    你只需要判断一下这个int 的值然后把结果写进viewbag里面返回到view。

    然后在页面里面显示这个viewbag的值就行了。

    具体你可以参照如下代码:

    Html:

    @model MVCTEST.Models.Sut
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div> 
            @using (Html.BeginForm("Save", "Create", FormMethod.Post))
            {
                @Html.TextBoxFor(m => m.StuId)
                @Html.TextBoxFor(m => m.StuName)
                <!--
            <input type="password" name="Password" />
            You can use HTML inputs, but it's more reliable to use strongly-typed MVC helpers.
            -->
                <input type="submit" value="Save" />
            }
        </div>
        @if (ViewBag.Message != null)
        {
            @Html.Label("Set from controller",(string)ViewBag.Message);
        }
    
    </body>
    </html>


    控制器:

     public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Save(Sut model)
            {
                int i = 0;
                string consString = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Student (Id,Name)VALUES(@Id, @name)"))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Id", model.StuId);
                        cmd.Parameters.AddWithValue("@name", model.StuName);
                        con.Open();
                       i = cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
                if (i != 0 && i != -1)
                {
                    ViewBag.Message = "Success";
                }
                // if credentials are correct.
                return View("Index");
              
            }

    结果:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年11月25日 7:55

全部回复

  • 你好,

    据我所知,如果你使用ADO.NET或者EF执行插入操作后,会返回一个int的值。

    你只需要判断一下这个int 的值然后把结果写进viewbag里面返回到view。

    然后在页面里面显示这个viewbag的值就行了。

    具体你可以参照如下代码:

    Html:

    @model MVCTEST.Models.Sut
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div> 
            @using (Html.BeginForm("Save", "Create", FormMethod.Post))
            {
                @Html.TextBoxFor(m => m.StuId)
                @Html.TextBoxFor(m => m.StuName)
                <!--
            <input type="password" name="Password" />
            You can use HTML inputs, but it's more reliable to use strongly-typed MVC helpers.
            -->
                <input type="submit" value="Save" />
            }
        </div>
        @if (ViewBag.Message != null)
        {
            @Html.Label("Set from controller",(string)ViewBag.Message);
        }
    
    </body>
    </html>


    控制器:

     public ActionResult Index()
            {
                return View();
            }
    
            [HttpPost]
            public ActionResult Save(Sut model)
            {
                int i = 0;
                string consString = ConfigurationManager.ConnectionStrings["MySQLConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Student (Id,Name)VALUES(@Id, @name)"))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Id", model.StuId);
                        cmd.Parameters.AddWithValue("@name", model.StuName);
                        con.Open();
                       i = cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
                if (i != 0 && i != -1)
                {
                    ViewBag.Message = "Success";
                }
                // if credentials are correct.
                return View("Index");
              
            }

    结果:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2016年11月25日 7:55
  • 這方法已經夠便捷了吧

    ※在Controller裡

    
    
    public ActionResult YourAction()
    {
        //写表后.....
        return Content("<script>alert('你的完成提示');</script>");
    }



    ====以下是簽名檔,不是針對發帖者的回覆==== <br/> <a href="http://www.dotblogs.com.tw/shadow">My Blog 高級打字員的技術雲</a> <br/> /*提醒網友們理性和平討論,酸言酸語不只曝露你是憤青的直男癌,更是一種網路霸凌*/ <br/> /*If my concept is wrong ,please correct me.Thanks.*/

    2017年4月4日 22:25