MVC 【ASPX视图引擎】

 

新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空)、视图引擎(ASPX)

 

1、认识控制器Controller

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

 

用到模型数据时需要引用命名空间

            using   项目名.Models

 

 

2、认识模型Model

LinQ 放在model里面,

 

 

3、认识视图 View

 在返回视图的动作中右键添加视图

只有在 控制器中 ActionResult 类型的动作中才可以有视图
        public ActionResult Index()
        {
            return View();
        }

 

用到模型数据时需要引用命名空间(最上方)

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

             引入      命名空间             项目名.模板名

 

 

案例分析:

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 %>      -- 视图中接收

可以传任何类型的值,只能在本视图中传值,

 

posted @ 2017-07-02 14:47  唐宏昌  阅读(436)  评论(0编辑  收藏  举报