ASP.NET MVC:一個簡單MVC示例


示例編號mvc100010010

1,功能描述

   一個基於標准的ASP.NET MVC2.0的一個項目.主要功能有:用戶登錄,產品的操作,商品展示,添加產品,修改商品,刪除商品.

2,技術與環境

 

操作系統:

windows

開發語言:

C#

開發框架:

ASP.NET MVC 2.0

數據庫:

SQL Server

開發軟件:

Microsoft Visual Studio 2010

 

 

項目組長:

yuanbo

成員:

null

個人主頁:

http://www.cnblogs.com/ylbtech/

科研團隊:

ylbtech

教研團隊:

ylbtech

 

3,數據庫設計

數據關系圖:

guanxi

3.1,基本數據庫

   3.1.1  sql-mvc-basic.sql

-- =============================================
-- ylb_menu: MVC測試數據庫
-- datbase: db1
-- author: yuanbo
-- pubdate:2012-8-1
-- =============================================
use master
IF EXISTS (SELECT * 
       FROM   master..sysdatabases 
       WHERE  name = N'db1')
    DROP DATABASE db1
GO

CREATE DATABASE db1
GO
use db1
go

-- =============================================
-- ylb: 1,Users
-- remark: 用戶表
-- =============================================
create table Users
(
username varchar(100) primary key, --昵稱[PK]
userpass varchar(100) not null        --密碼    
)


go
-- =============================================
-- ylb: 2,Product
-- remark: 產品表
-- =============================================
create table Product
(
productId int primary key identity, --編號[PK]
productName varchar(100) not null,    --產品名稱
unitprice decimal(6,2) check(unitprice>0),    --單價
type varchar(100) check(type in('電器','水果'))    --類型
)

go
-- =============================================
-- ylb_test: 1,向"Users"表插入測試數據
-- remark: 測試數據
-- =============================================
insert into Users(username,userpass) values('yb','m123')

go
print 'mvc測試數據庫創建成功!'

3.2,插入測試數據

  無,在3.1.1已插入測試數據。

3.3,操作表步驟      

   3.3.1  1, Users.sql

View Code
-- =============================================
-- ylb_menu: MVC測試數據庫
-- table: 1,Users
-- remark:對用戶表的操作與步驟
-- author: yuanbo
-- pubdate:2012-8-1
-- =============================================
use db1

go
-- =============================================
-- ylb_test: 1,向"Users"表插入測試數據
-- remark: 測試數據
-- =============================================
insert into Users(username,userpass) values('yb','m123')

go
-- =============================================
-- ylb: 1,Login
-- remark: 用戶登錄
-- =============================================
select COUNT(*) from Users where username='yb' and userpass='m123'

  3.3.2  2, Product.sql

View Code
-- =============================================
-- ylb_menu: MVC測試數據庫
-- table: 1,Products
-- remark:對產品表的操作與步驟
-- author: yuanbo
-- pubdate:2012-8-1
-- =============================================
use db1

go
-- =============================================
-- ylb_test: 1,向"Users"表插入測試數據
-- remark: 測試數據
-- =============================================

go
-- =============================================
-- ylb: 1,GetAll
-- remark: 獲取所有產品,並以productId降序排列
-- =============================================
select productId,productName,unitPrice,type from Product order by productId desc

go
-- =============================================
-- ylb: 2,Add
-- remark: 添加一個產品
-- field: productName,unitPrice,type
-- =============================================
select productName,unitPrice,type from Product
go
insert into Product(productName,unitPrice,type) values()

go
-- =============================================
-- ylb: 3,GetModel
-- remark: 獲得一個實體對象,根據productId
-- =============================================
select productId,productName,unitPrice,type from Product where productId=0

go
-- =============================================
-- ylb: 4,Update
-- remark: 修改一條信息 ,根據productId
-- =============================================
update Product set productName='yb',unitPrice='2.3',type='電器' where productId=0

go
-- =============================================
-- ylb: 5,Delete
-- remark: 刪除一條信息,根據productId
-- =============================================
delete Product where productId=0
4,功能截圖

 4.1,前台

4.1.1 用戶登錄(/Views/Account/Login.aspx)

login
4.1.2 商品展示(/Views/Product/Index.aspx)

Show Products
4.1.3 添加商品(/Views/Product/Create.aspx)

Add Product
4.1.4 修改商品(/Views/Product/Edit.aspx)

Update Product
4.1.5 刪除商品(/Views/Product/Index.aspx)     

Delete Product    

4.2,后台

   無后台。

5,代碼分析

5.1,前台

  5.1.1 [只有一個示例展示,更多請下載百度文庫示例案例…] 即,/Account

  5.1.1.1_M_Info    /Models/UsersInfo.cs

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc1.Models
{
    public class UsersInfo
    {
        /// <summary>
        /// username
        /// </summary>
        public string Username { get;set;}
        /// <summary>
        /// password
        /// </summary>
        public string Userpass { get; set; }
    }
}

  5.1.1.1_M_Oper  /Models/Users.cs

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data.SqlClient;
namespace Mvc1.Models
{
    public class Users
    {

        /// <summary>
        /// ylb: 1,Login
        /// remark: 用戶登錄
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpass"></param>
        /// <returns></returns>
        public bool Login(string username, string userpass)
        {
            bool b = false;

            string sql = "select COUNT(*) from Users where username=@username and userpass=@userpass";

            SqlConnection conn = new DBConnection().Conn;
            SqlCommand com = conn.CreateCommand();

            com.Parameters.Add(new SqlParameter("@username", username));
            com.Parameters.Add(new SqlParameter("@userpass", userpass));
            com.CommandText = sql;

            conn.Open();
            try
            {
                Object obj = com.ExecuteScalar();
                int count = Convert.ToInt32(obj);
                if (count > 0)
                    b = true;
            }
            finally
            {
                conn.Close();
            }
            return b;
        }
    }
}

  5.1.1.1_V  /Views/Login.aspx

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

<!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>Login</title>
</head>
<body>
    <div>
    <fieldset>
    <div><%=ViewData["LoginIsFail"] %></div>
    <legend>Login</legend>
    <form action="/Account/Login" method="post">
    username:<br />
    <input id="username" name="username" value="yb" /><br />
    password:<br />
    <input id="userpass" name="userpass" value="m123" /><br />
    <button type="submit">Login</button>
    </form>
    </fieldset>
    </div>
</body>
</html>

  5.1.1.1_C  /Controllers/AccountController.cs

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Mvc1.Models;
namespace Mvc1.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/

        public ActionResult Index()
        {
            return View("Login");
        }

        //
        // GET: /Account/Login
        public ActionResult Login(string username,string userpass)
        {

            if (new Users().Login(username, userpass))
                return RedirectToAction("Index", "Product");
            else
            {
                ViewData["LoginIsFail"] = "User name or password is incrorenct.";
                return View();
            }
        }

        
    }
}

5.2,后台

   無。

6,示例|講解案例下載

百度文庫開發文檔:

http://wenku.baidu.com/view/4cb28c88cc22bcd126ff0c5a.html

谷歌開源代碼下載:

http://code.google.com/p/ylbtechaspnetmvc/downloads/list

請單擊“ylbtech ASP.NET MVC100010010”

百度網盤  http://pan.baidu.com/s/1i49zn73

請單擊“ASP.NET MVC100010010”

warn 作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM