Beispiel #1
0
        /**
         * <summary>
         * This is the protected method SaveButton_Click as an event
         * </summary>
         *
         * @method SaveButton_Click
         * @returns {void}
         * @param {object} sender
         * @param {EventArgs} e
         */
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // Use EF to conect to the server
            using (TodoContext1 db = new TodoContext1())
            {
                // use the todo model to create a new todo object and
                // save a new record

                Todo newtodo = new Todo();



                newtodo.TodoDescription = todoname.ToString();
                newtodo.TodoNotes       = todoname.ToString();
                newtodo.Completed       = false;

                db.Todos.Add(newtodo);


                // save our changes - also updates and inserts
                db.SaveChanges();

                // Redirect back to the updated todolist page
                Response.Redirect("~/TodoList.aspx");
            }
        }
Beispiel #2
0
        /**
         * <summary>
         * This is the protected method GridViewDataBind to bind data with gridview
         * </summary>
         *
         * @method GridViewDataBind
         * @returns {void}
         */
        protected void GridViewDataBind()
        {
            // connect to EF DB
            using (TodoContext1 db = new TodoContext1())
            {
                string SortString = Session["SortColumn"].ToString() + " " +
                                    Session["SortDirection"].ToString();
                // query the Todos Table using EF and LINQ
                var todoList = (from todolist in db.Todos
                                select new
                {
                    TodoID = todolist.TodoID,
                    TodoDescription = todolist.TodoDescription,
                    TodoNotes = todolist.TodoNotes,
                    Completed = todolist.Completed
                }).ToList();



                // bind the result to the TodoListGridView
                TodoListGridView.DataSource = todoList.AsQueryable().OrderBy(SortString).ToList();
                TodoListGridView.DataBind();
                TodoListGridView.Columns[0].Visible = false;
            }
        }
 public List <TodoItem1> Get()
 {
     using (TodoContext1 t1 = new TodoContext1())
     {
         var todo = t1.TodoItems.ToList();
         return(todo);
     }
 }
        public ActionResult <IEnumerable <string> > PostWeatherForecast(TodoItem1 todoitem1)
        {
            var database = new TodoContext1();

            database.TodoItems.Add(new TodoItem1 {
                Id = todoitem1.Id, Name = todoitem1.Name, IsComplete = todoitem1.IsComplete
            });
            database.SaveChanges();
            return(new string[] { "value1", "value2" });
        }
Beispiel #5
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            using var database = new TodoContext1();    //新增
            database.Database.EnsureCreated();
            services.AddDbContext <TodoContext1>(opt =>
                                                 opt.UseSqlite("TodoList"));


            //如果没有创建数据库会自动创建,最为关键的一句代码
            services.AddControllers();
        }
Beispiel #6
0
        /**
         * <summary>
         * This is the protected method TodoListGridView_RowDeleting to delete data
         * </summary>
         *
         * @method TodoListGridView_RowDeleting
         * @returns {void}
         * @param {object} sender
         * @param {GridViewDeleteEventArgs} e
         */
        protected void TodoListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // store which row was clicked
            int selectedRow = e.RowIndex;

            // get the selected todoid using the Grid's DataKey collection
            int todoID = Convert.ToInt32(TodoListGridView.DataKeys[selectedRow].Values["TodoID"]);

            // use EF and LINQ to find the selected student in the DB and remove it
            using (TodoContext1 db = new TodoContext1())
            {
                // create object ot the todo clas and store the query inside of it
                Todo tododelet = (from todorecord in db.Todos
                                  where todorecord.TodoID == todoID
                                  select todorecord).FirstOrDefault();

                // remove the selected todo from the db
                db.Todos.Remove(tododelet);

                // save my changes back to the db
                db.SaveChanges();
            }
        }
 public TodoItem1Controller(TodoContext1 context)
 {
     _context = context;
 }