• MVC开发基础


    新建--项目--ASP.NET MVC 4 WEB 应用程序

    MVC:

    M--Model  模型层     放置数据访问类,linq

    V--View  视图层       界面层   aspx文件。只有一个页面文件,没有C#代码文件  ,那怎么执行?

    <%@ %>引用命名空间

    <% %>在此区域里嵌所有C#代码

    <%@ %>输出一个变量的值

    C--Control  控制层   添加-控制器--

    C层:由控制器及控制器中的动作所组成,MVC请求不是直接对页面,而是对控制器中的某个动作发送请求

    默认返回  Home控制器下的Index动作

    如果有请求的控制器,没有请求动作。那么会默认返回

    数据展示:

    namespace mvc1.Controllers
    {
        public class HomeController : Controller
        {
            
            //动作
            public string Index()
            {
                return "hole word";
            }
            public string Other1()
            {
                return "<html><head></head><body><h1>"+DateTime.Now+"<h1></body></html>";
            }
        }
    }

    qidian1Controller.cs:

    namespace mvc1.Controllers
    {
        public class qidianController : Controller
        {
            
    
            public string Index()
            {
                StringBuilder ss = new StringBuilder();
                using (DataClasses1DataContext con = new DataClasses1DataContext())
                {
                    List<Users> ulist = con.Users.ToList();
                    foreach(Users u in ulist)
                    {
                    ss.Append(u.NickName+"|");//追加
                    }
                }
                return ss.ToString();
            }
            public ActionResult other1()//添加视图--右键第一个(会出现一个html:other1.aspx)
            {
                return View();
            }

    other1.aspx:

    <%@ Import Namespace="mvc1.Models" %>    <%--引用命名空间--%>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <meta name="viewport" content="width=device-width" />
        <title>other1</title>
    </head>
    <body>
        <div>
            <h1>这是视图产生出的页面</h1>
             <table>
                <tr>
                    <td>用户名</td>
                     <td>密码</td>
                     <td>昵称</td>
                     <td>性别</td>
                     <td>生日</td>
                    <td>民族</td>
                </tr>
            <%
               
                List<Users> ulist = new UsersData().Select();//在model里添加一个类,来这调用
                    foreach(Users u in ulist)
                    {
                 %>
           
    
                <tr>
                    <td><%=u.UserName %></td>
                    <td><%=u.Password %></td>
                    <td><%=u.NickName %></td>
                    <td><%=Convert.ToBoolean(u.Sex)?"男":"女" %></td>
                    <td><%=u.Birthday.Value.ToString("yyyy年MM月dd日") %></td>
                    <td><%=u.Nation1.NationName %></td>
                </tr>
                 <%
                     
                 } %>
            </table>
        </div>
    UsersData类:

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

     添加:

    添加控制器,添加一个动作(视图Insert()),--添加视图(写添加界面)--保存按钮:加一个动作Insert1(),接收内容:1、组对象2、往数据库添加数据3、返回展示页面 RedirectToAction()

    <body>
     <form action="insert1" method="post">
        <h1>添加页面</h1><%--name--%>
        用户名:<input  type="text" name="username"/><br /><br />
        
        密码:<input  type="text"/ name="password"><br /><br />
        昵称:<input  type="text" name="nickname" /><br /><br />
        性别:<input  type="text" name="sex"/><br /><br />
        生日:<input  type="text" name="birthday"/><br /><br />
        民族:<input  type="text" name="nationcode"/><br /><br />
        <input  type="submit" value="保存"/>
           </form>
    </body>
            public ActionResult Insert()//添加页面
            {
                return View();
            }
    
            public ActionResult insert1(string username,string password,string nickname,bool sex,DateTime birthday,string nationcode)//添加保存按钮,传值
            {
                Users u = new Users();
                u.UserName = username;
                u.Password = password;
                u.NickName = nickname;
                u.Sex = sex;
                u.Birthday = birthday;
                u.NationCode = nationcode;
                new UsersData().Insert(u);//models里写方法直接来调用
                return RedirectToAction("Index","Home");//添加完了,再跳回显示页面
            }

     删除:

     <td>
                   <a href="Home/Update/<%=u.UserName %>">修改</a>
                   <a href="Home/Delete/<%=u.UserName %>">删除</a><%--用这个方式传值--%>
               </td>
        public ActionResult Delete(string id)//获取值
            {
                new UsersData().Delete(id);//uname是靠id这个路由传过来的
                return RedirectToAction("Index");
            }

    修改:

     <form action="/Home/Update1" method="post">
    
            <%
                Users u = ViewBag.hehe as Users;
    
                if (u != null)
                {
            %>
    
            <h1>修改页面</h1>
            用户名:<input type="text" name="username" value="<%=u.UserName %>" /><br />
            <br />
            密码:<input type="text" name="password" value="<%=u.Password %>" /><br />
            <br />
            昵称:<input type="text" name="nickname" value="<%=u.NickName %>" /><br />
            <br />
            性别:<input type="text" name="sex" value="<%=u.Sex %>" /><br />
            <br />
            生日:<input type="text" name="birthday" value="<%=u.Birthday %>" /><br />
            <br />
            民族:<input type="text" name="nationcode" value="<%=u.NationCode %>" /><br />
            <br />
            <input type="submit" value="保存" />
            <%
                }
                else
                {
            %>
            <h1>未查到学生信息!</h1>
            <%
                }
            %>
        </form>
      public ActionResult Update(string aa)
            {
                Users uu = new UsersData().Select(aa);
                ViewBag.hehe = uu;//获取数据传递。数据包
                return View();
            }
            public ActionResult Update1(Users u)//返回一个对象
            {
                new UsersData().Update(u);
                return RedirectToAction("Index");//跳回主页面
            }
  • 相关阅读:
    SOFA 源码分析 — 自动故障剔除
    Pod——状态和生命周期管理及探针和资源限制
    pause的作用
    k8s-部署策略
    linux-删除一个目录下的所有文件,但保留某个或者多个指定文件
    k8s-gitlab搭建
    git中报unable to auto-detect email address 错误的解决拌办法
    k8s-secret用法
    k8s-traefik默认80端口
    nginx和apache区别
  • 原文地址:https://www.cnblogs.com/yp11/p/6036915.html
Copyright © 2020-2023  润新知