【问题标题】:WebForm Data Binding to SQL DatabaseWebForm 数据绑定到 SQL 数据库
【发布时间】:2017-07-07 12:05:14
【问题描述】:

我正在按照本教程学习 WebForms 的基础知识:

https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/updating-deleting-and-creating-data

我已经能够将网站中的数据直接插入数据库。我现在面临另一个问题。按下表单的按钮后,我想在我的代码中获取插入的数据,以便我可以执行一些计算,然后将数据插入数据库。

是否可以通过教程中建议的方法来实现?

【问题讨论】:

    标签: sql webforms


    【解决方案1】:

    您应该使用addStudentForm_InsertItem() 方法,如下所示。

    public void addStudentForm_InsertItem()
    {
        var item = new Student();
    
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
    

    //在此处进行修改、计算,并设置项目的更新属性。

            using (SchoolContext db = new SchoolContext())
            {
                db.Students.Add(item);
                db.SaveChanges();
            }
        }
    }
    

    【讨论】:

    • 谢谢!!这似乎是最好的解决方案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多