Creating a Tasklist Application with ASP.NET MVC

Creating a Tasklist Application with ASP.NET MVC

1.Create Home controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TaskList.Controllers
{
    public class HomeController : Controller
    {
        private TaskListDBEntities db;
        //Display a list of tasks
        public ActionResult Index()
        {
            return View();
        }
        //Display a form for creating a new task
        public ActionResult Create()
        {
            return View();
        }
        //Adding a new task to the database
        public ActionResult CreateNew(string task)
        {
            //Add the new task to database
            Tasks newTask = new Tasks
            {
                
                Task=task,
                IsCompleted=false,
                EntryDate=DateTime.Now,
                
                
            };
          
            db.Tasks.AddObject(newTask);
            db.SaveChanges();

            
            return RedirectToAction("Index");
        }
        //Mark a task as complete
        public ActionResult Complete()
        {
            return RedirectToAction("Index");
        }
    }
}

2.Create Home and Create View
Home
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <div>
    <h1>My Tasks</h1>
    ......displaying all tasks
    <br /><br />
    <a href="/Home/Create">Add new Task</a>

    </div>
</body>
</html>

Create
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <div>
    <h1>Add New Task</h1>
    <form method="post" action="/Home/CreateNew">
    <label for="task">Task:</label>
    <input type="text" name="task" />
    <br />
    <input type="submit" value="Add Task" />
    </form>
    </div>
</body>
</html>

3.Add ADO Entity Framework

4.

你可能感兴趣的:(Creating a Tasklist Application with ASP.NET MVC)