您的位置:首页 > 移动开发

Creating a Tasklist Application with ASP.NET MVC

2013-11-02 21:50 369 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐