zoukankan      html  css  js  c++  java
  • MVC学习笔记一

       在园子里面查找了些MVC的资料。通过Demo来加强对MVC知识点的学习,归纳总结了几点:

       一、访问方式:http:ip(localhost):port/ControllerName/MethodName?argName

       二、  C#中的绑定和变量在aspx/cshtml的显示:

       1、C#:ViewBag.Name=value/method;         aspx/cshtml:@ViewBag.name

       2、C#:ViewData["Name"]=value/method;    aspx/cshtml:@(@ViewData["name"] as string) //类型转换

      三、 通过控制器的方法名称生成对应的视图,视图名称和控制器的名称最好对应。

      四、在Model中声明对象的属性对应数据库字段,同时可声明字段类型如: [Key]=》【主键】、 [Required]=》必填 etc。

      五、通过引用using System.Data.Entity; 命名空间使得model实体对象继承 : DbContext。

            在实体对象类里面声明对象如: public DbSet<Student> stuList { get; set; }

      六、创建视图可更改名称、选择对应的Model类、引用脚步、操作的模版如:CRUD、是否是分布视图、选择的模版及占用的内容页对应的位置。

      七、采用Entity Framework的CodeFrist生成对应的数据库表和数据库名称、

            数据库名称由对象实体的空间名称和类名构成如:Mvc3Demo.Models.StudentEntities

      八、通过linq的方式关联2张表如班级和学生表: var stulist = db.stuList.Include(s => s.scClass);

      九、下拉菜单的绑定方法一:

             ViewBag.外键ID=new SelectList(关联类集合,”关联类的ID”,”关联类中要显示的名称”,” 当前类中外外键ID用来作为被选中的值”)

             页面调用 方法:@Html.DropDownList("外键ID", String.Empty)

            下拉菜单的绑定方法二:

           ViewBag.外键ID=关联类集合.OrderBy(m=>m.ColumnName).ToList();

            页面调用:@Html.DropDownList("外键ID", new SelectList(ViewBag.集合 as         

           System.Collections.IEnumerable,"ID","ColumnName",Model.外键ID))

          

      十、通过生成CRUD我们可以看到Edit方法其中一个有 [HttpPost],只是修改后表单提交post方式处理的方法,

            而没有的那个是修改前Get方式查询的方法。

           备注:常用的数据注解如下:摘自:某网友

            [Display(Name = "姓名")]  //显示的名称
            [Required(ErrorMessage = "您需要填写{0}")]  //必须字段 错误提示
            [StringLength(50, MinimumLength = 3)]     //设置最大长度和最小长度
            [Range(1, 150, ErrorMessage = "年龄填写不正确!")]  //设置 值范围
            [Range(typeof(decimal), "50.00", "250.00", ErrorMessage = "高超出指定范围")]
            [DataType(DataType.Date, ErrorMessage = "{0}格式不正确")]  //设置数据类型以及错误提示
            [DataType(DataType.MultilineText)] //多行文本
            [RegularExpression(@"(w)+(.w+)*@(w)+((.w+)+)", ErrorMessage = "{0}格式不正确")]  //正则验证
            [DataType(DataType.Password)] //密码验证

            [DataType(DataType.Currency)]//金额验证

             [DisplayFormat(DataFormatString = "{0:d}")]日期验证如出版日期格式

             [DisplayFormat(DataFormatString = "{0:c}")]金额的显示格式的验证

            [DataType(DataType.EmailAddress, ErrorMessage = "{0}格式不正确")]  //邮箱格式验证
            [Compare("Email", ErrorMessage = "{0}两次输入不一致")]   //设置比较两个字段的值(自定义)
            [Remote("CheckPhone", "StuInfo", ErrorMessage = "{0}已被注册")]   //在指定的Conteroller中的通道(route)中验证数据[自定义]

           * 页面验证方式如:  @Html.ValidationMessageFor(model => model.age)。附控制器代码:

           *Action验证方法:通过使用ModelState.IsValid来验证Action 方法

      private StudentEntities db = new StudentEntities();
    
            //
            // GET: /StudentManager/
    
            public ViewResult Index()
            {
                var stulist = db.stuList.Include(s => s.scClass);
                return View(stulist.ToList());
            }
    
            //
            // GET: /StudentManager/Details/5
    
            public ViewResult Details(int id)
            {
                Student student = db.stuList.Find(id);
                return View(student);
            }
    
            //
            // GET: /StudentManager/Create
    
            public ActionResult Create()
            {
                ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className");
                return View();
            }
    
            //
            // POST: /StudentManager/Create
    
            [HttpPost]
            public ActionResult Create(Student student)
            { //通过使用ModelState.IsValid来验证Action 方法
                if (ModelState.IsValid)
                {
                    db.stuList.Add(student);
                    db.SaveChanges();
                    return RedirectToAction("Index");
            
    
                }
                //绑定外键形成下拉菜单
                ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className", student.scClassID);
                return View(student);
            }
    
            //
            // GET: /StudentManager/Edit/5
    
            public ActionResult Edit(int id)
            {
                Student student = db.stuList.Find(id);
                ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className", student.scClassID);
    
                return View(student);
            }
    
            //
            // POST: /StudentManager/Edit/5
    
            [HttpPost]
            public ActionResult Edit(Student student)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(student).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                ViewBag.scClassID = new SelectList(db.scClassList, "ID", "className", student.scClassID);
                return View(student);
            }
    
            //
            // GET: /StudentManager/Delete/5
    
            public ActionResult Delete(int id)
            {
                Student student = db.stuList.Find(id);
                return View(student);
            }
    
            //
            // POST: /StudentManager/Delete/5
    
            [HttpPost, ActionName("Delete")]
            public ActionResult DeleteConfirmed(int id)
            {
                Student student = db.stuList.Find(id);
                db.stuList.Remove(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    

      

       

  • 相关阅读:
    notepad++ 安装
    Git 安装
    C 字符串常量 数据类型
    vue路由传参query和params的区别
    mysql 在 centos 上安装,做成服务,且开机启动 步骤
    全网最详细Apache Kylin1.5安装(单节点)和测试案例 ---> 现在看来 kylin 需要 安装到Hadoop Master 节点上
    Kylin build cube step 2 报错(Kylin 安装在slave 节点上)
    Kylin build cube step 2 报错
    Kylin 环境 搭建 成功
    Kylin 环境 搭建 报错
  • 原文地址:https://www.cnblogs.com/professional-NET/p/4765182.html
Copyright © 2011-2022 走看看