MVC 【ASPX視圖引擎】

 

新建項目----ASP.NET MVC 4 Web 應用程序------選擇模板(空)、視圖引擎(ASPX)javascript

 

一、認識控制器Controllerhtml

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MVC1.Models; namespace MVC1.Controllers    //命名空間
{ //控制器名稱 //繼承Controller
    public class HomeController : Controller { //action 動做 每個動做決定你要幹什麼(方法) ActionResult返回房類型
        public string Index() { return "你好!世界"; } } }
View Code

 

用到模型數據時須要引用命名空間java

            using   項目名.Modelsmvc

 

 

二、認識模型Modelide

LinQ 放在model裏面,post

 

 

三、認識視圖 Viewspa

 在返回視圖的動做中右鍵添加視圖3d

只有在 控制器中 ActionResult 類型的動做中才能夠有視圖
        public ActionResult Index()
        {
            return View();
        }

 

用到模型數據時須要引用命名空間(最上方)code

    <% @ Import Namespace ="mvc2.Models"  %>orm

             引入      命名空間             項目名.模板名

 

 

案例分析:

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

namespace mvc2.Models
{
    public class UsersData
    {
        DataClasses1DataContext con = new DataClasses1DataContext();
        public List<Users> SelectAll()
        {
            return con.Users.ToList();
        }

        public bool Insert(Users u)
        {
            bool ok = false;
            try
            {
                con.Users.InsertOnSubmit(u);
                con.SubmitChanges();
                ok = true;
            }
            catch { }

            return ok;
        }

        public bool DeleteUser(string ids)
        {
            bool ok = false;

            Users u = con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault();
            if (u != null)
            {
                con.Users.DeleteOnSubmit(u);
                con.SubmitChanges();
                ok = true;
            }

            return ok;
        }

        public Users SelectUser(string ids)
        {
            return con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault();
        }



    }
}
UsersData

 

using mvc2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mvc2.Controllers
{
    public class HomeController : Controller
    {
        //展現
        public ActionResult Index()
        {
            return View();
        }

        //添加視圖
        public ActionResult Insert()
        {
            return View();
        }

        //添加計算
        public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation)
        {
            Users u = new Users();
            u.UserName = username;
            u.PassWord = password;
            u.NickName = nickname;
            u.Sex = Convert.ToBoolean(sex);
            u.Birthday = Convert.ToDateTime(birthday);
            u.Nation = nation;

            bool ok = new UsersData().Insert(u);
            Session["InsertOK"] = ok;

            return RedirectToAction("Index", "Home");
                      //重定向    (  動做名 ,  控制器名 )
        }


        //刪除
        public ActionResult Delete(string id)
        {
            bool ok = new UsersData().DeleteUser(id);

            return RedirectToAction("Index");
        }


        //修改
        public ActionResult Update(string id)
        {
            Users u = new UsersData().SelectUser(id);  //根據id查詢出u

            ViewBag.hehe = u;       //試圖數據包,傳遞數據

            return View();
        }

    }
}
控制器

 

   --刪除,不須要視圖展現,直接在動做上 計算

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

<%@ Import Namespace="mvc2.Models" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <% if (Convert.ToBoolean(Session["InsertOK"]) == true)
           {%>

        <script type="text/javascript">
            alert('添加成功!');

        </script>
        <%
               Session["InsertOK"] = null;
           } %>

        <table style="width: 100%; text-align: center; background-color: navy;">
            <tr style="color: white;">
                <td>用戶名</td>
                <td>密碼</td>
                <td>暱稱</td>
                <td>性別</td>
                <td>生日</td>
                <td>民族</td>
                <td>操做</td>
            </tr>
            <%
                List<Users> ulist = new UsersData().SelectAll();

                foreach (Users u in ulist)
                {
            %>
            <tr style="background-color: white;">
                <td><%=u.UserName %></td>
                <td><%=u.PassWord %></td>
                <td><%=u.NickName %>同窗</td>
                <td><%=u.Sex.Value?"":"" %></td>                       <%--.value 數據轉換--%>
                <td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td>
                <td><%=u.UserNation.NationName %></td>
                <td>
                    <a href="Home/Update/<%=u.Ids %>">修改</a> | 
                    <a href="Home/Delete/<%=u.Ids %>">刪除</a>

                </td>
            </tr>
            <%
                }
            %>
        </table>
        <input type="button" value="添加" onclick="window.open('Home/Insert', '_blank');" />

    </div>
</body>
</html>
主展現視圖

 

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

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Insert</title>
</head>
<body>
    <div>
        <h1>添加用戶</h1>

        <form action="Insert1" method="post">  <%--表單--%>
                 <%-- 提交到哪     提交方式--%>

            用戶名:
        <input type="text" name="username" /><br />
            密碼:
        <input type="text" name="password" /><br />
            暱稱:
        <input type="text" name="nickname" /><br />
            性別:
        <input type="text" name="sex" /><br />
            生日:
        <input type="text" name="birthday" /><br />
            民族:
        <input type="text" name="nation" /><br />
            <input type="submit" value="保存" />



        </form>

    </div>
</body>
</html>
添加——用表單提交

 

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

<%@ Import Namespace="mvc2.Models" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>Update</title>
</head>
<body>

    <h1>修改用戶</h1>

    <%  Users u = ViewBag.hehe;  %>  
                        <%-------------將傳進來的數據取出--%>


    <form action="Update1" method="post">
        用戶名:                                       <%-----------綁定數據--%>
        <input type="text" name="username" value="<%=u.UserName %>" /><br />
        密碼:
        <input type="text" name="password" value="<%=u.PassWord %>" /><br />
        暱稱:
        <input type="text" name="nickname" value="<%=u.NickName %>" /><br />
        性別:
        <input type="text" name="sex" value="<%=u.Sex %>" /><br />
        生日:
        <input type="text" name="birthday" value="<%=u.Birthday %>" /><br />
        民族:
        <input type="text" name="nation" value="<%=u.Nation %>" /><br />
        <input type="submit" value="保存" />


    </form>

</body>
</html>
修改——表單提交

 

ViewBag 傳值

    ViewBag.變量=值

                         ViewBag.a=u;                            --  動做中傳

                        <% Users u = ViewBag.a %>      -- 視圖中接收

能夠傳任何類型的值,只能在本視圖中傳值,

相關文章
相關標籤/搜索