Mvc

  1.Html 增加扩展方法

 using System.Web.Mvc;

 namespace KnockoutBootstrapMvc.Entensions
{
public static class HtmlHelperExtensions
{
public static HtmlString HtmlConvertToJson(this HtmlHelper htmlHelper, object model)
{
var setting = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
};
return new HtmlString(JsonConvert.SerializeObject(model, setting));
}
}
}

 2.Code First

创建Entity实体,当有外键关联时,可以在类内部创建virtual ObjClass  如果是数组为ICollection<ObjectClass>

  public class Author
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Biography { get; set; }
public virtual ICollection<Book> Books { get; set; }
} public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public string Isbn { get; set; }
public string Synopsis { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public virtual Author Author { get; set; } }

3.Db类库\重写OnModelCreating方法

创建Db类继承DbContext,

 创建无参构造函数,设置父类连接字符串的名称

 创建实体对象  Public DbSet<EntityName> PropertyName {get;set;}

 重写OnModelCreating方法,此方法内在实体类创建时触发,可以设置实体的创建格式  如:开启、关闭默认的Id列为主键,是否自增标识列等。

  public class BookContext:DbContext
{
public BookContext()
:base("BookContext")
{ } public DbSet<Book> Books { get; set; }
public DbSet<Author> Authors { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
} }

4.创建数据库启动程序类库,集成DropCreateDataBaseIfModelChanges<T> 、重写Seed方法用来初始化表格信息、向数据库表格中插入数据。

  未直接save的表,在save关联表时,数据也会插入进去

 public class BookInitializer : DropCreateDatabaseIfModelChanges<BookContext>
{
protected override void Seed(BookContext context)
{
var author = new Author()
{
Biography = "..",
FirstName = "zang",
LastName = "san"
};
var books = new List<Book>()
{
new Book()
{
Author=author,
Description="...",
ImageUrl="http://ecx.images-amazon.com",
Isbn="",
Title="Knokout.js",
Synopsis="..." },
new Book()
{
Author=author,
Description="...",
ImageUrl="http://ecx.images-amazon.com",
Isbn="",
Title="bootstrap.css",
Synopsis="..."
}
,
new Book()
{
Author=author,
Description="...",
ImageUrl="http://ecx.images-amazon.com",
Isbn="",
Title="asp.net mvc",
Synopsis="..."
}
}; author = new Author()
{
Biography = "..",
FirstName = "li",
LastName = "si"
};
books.Add(new Book()
{
Author = author,
Description = "...",
ImageUrl = "http://ecx.images-amazon.com",
Isbn = "",
Title = "lisi asp.net mvc",
Synopsis = "..."
}); books.ForEach(b => context.Books.Add(b));
context.SaveChanges();
//base.Seed(context); //new BookInitializer();
} }

    在应用启动方法中设置数据库对象,

  1.Global.asax中创建Db对象

  2.DataBase.SetInitialzer(new Objclass);

  3.Db对象.DataBase.Initialize(true);

   namespace KnockoutBootstrapMvc
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); var bookContext = new BookContext();
Database.SetInitializer(new BookInitializer());
bookContext.Database.Initialize(true); }
}
}

5.Controller 方法属性前可以使用HttpPost等定义访问请求格式及是否启用验证

   // POST: Authors/Create
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,Biography")] Author author)
{
if (ModelState.IsValid)
{
db.Authors.Add(author);
db.SaveChanges();
return RedirectToAction("Index");
} return View(author);
}

 6.@sections 定义标签,在Layout页面里可以使用 @RenderSection(name:required:) 来加载对应的标签

 

PageA.cshtml
@section scripts
{
<script type="text/javascript"> function ViewModel(authors)
{
var self = this;
self.authors = authors;
} var viewModel = new ViewModel(@Html.HtmlConvertToJson(Model));
ko.applyBindings(viewModel);
</script> } Layout.cshtml
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<script type="text/javascript" src="~/Scripts/knockout-3.4.0.js"></script>
@RenderSection( name:"scripts", required: false)

KnocoutJs+Mvc+BootStrap 学习笔记(Mvc)的更多相关文章

  1. 2.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用. 摘要 ...

  2. ASP.NET Core MVC 网站学习笔记

    ASP.NET Core MVC 网站学习笔记 魏刘宏 2020 年 2 月 17 日 最近因为” 新冠” 疫情在家办公,学习了 ASP.NET Core MVC 网站的一些知识,记录如下. 一.新建 ...

  3. Bootstrap~学习笔记索引

    回到占占推荐博客索引 bootstrap已经用了有段时间了,感觉在使用上还是比较容易接受的,在开发人员用起来上,也还好,不用考虑它的兼容性,手机,平台,PC都可以有效的兼容. bootstrap官方a ...

  4. Bootstrap学习笔记(二) 表单

    在Bootstrap学习笔记(一) 排版的基础上继续学习Bootstrap的表单,编辑器及head内代码不变. 3-1 基础表单 单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文 ...

  5. bootstrap学习笔记之为导航条添加标题、二级菜单及状态 http://www.imooc.com/code/3120

    为导航条添加标题.二级菜单及状态 加入导航条标题 在Web页面制作中,常常在菜单前面都会有一个标题(文字字号比其它文字稍大一些),其实在Bootstrap框架也为大家做了这方面考虑,其通过" ...

  6. bootstrap学习笔记之基础导航条 http://www.imooc.com/code/3111

    基础导航条 在Bootstrap框中,导航条和导航从外观上差别不是太多,但在实际使用中导航条要比导航复杂得多.我们先来看导航条中最基础的一个--基础导航条. 使用方法: 在制作一个基础导航条时,主要分 ...

  7. Bootstrap学习笔记-布局

    Bootstrap学习笔记-布局 默认是响应式布局,就是你在改变页面的时候也不会出现乱的现象. <html><head> <meta charset="utf- ...

  8. 3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html 概述 在文章:<[Spring学习笔记-MVC-3]SpringMVC返回Json数据-方 ...

  9. 1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:

    转自:https://www.cnblogs.com/ssslinppp/p/4528892.html [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://w ...

随机推荐

  1. Linux 下的dd命令使用详解(摘录)【转】

    转自:https://www.cnblogs.com/jikexianfeng/p/6103500.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意 ...

  2. 用ARX自定义实体

      本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h",&quo ...

  3. oracle监控

    python代码 #!/usr/bin/env python # -*- coding: UTF-8 -*- import subprocess import sys import re def ru ...

  4. Html input 常见问题

    1.input回车事件不执行导致页面刷新 场景:在文本框中输入关键字按回车,页面自动刷新了 <form name="keywordForm" method="pos ...

  5. macbook 安装win7

    长按option进入启动选择页面,选择winpe后进入安装,与普通电脑没有区别.安装后需要安装苹果专用驱动程序Boot Camp,才可以正常使用触控板等驱动

  6. 029_mount bind挂载

    一. 由于公司的配置标准并不统一,交付的磁盘挂载的路径不是想要的路径,但是 1./home目录下有很重要的堡垒机登录的相关文件,还不能卸载 2.我通过pts/0登录的,这个文件描述符也是在/home目 ...

  7. web@HTML重要标签详介绍.

    1.table标签<table border="1px " rules="groups" cellpadding="5px" cell ...

  8. mysql5.7 pxc

    pxc优点总结:可以达到时时同步,无延迟现象发生完全兼容MySQL对于集群中新节点的加入,维护起来很简单数据的强一致性不足之处总结:只支持Innodb存储引擎存在多节点update更新问题,也就是写放 ...

  9. Light Oj 1005

    题意: 从 n*n 的棋盘中放置 K 个 行和列不冲突的棋子 思路: 组合数学, 先选 k 个 行, k 个列, 就是 C(n,k) ^ 2; 然后 K 个棋子不相同, K ! 全排列 #includ ...

  10. response.setContentType()的String参数及对应类型

    response.addHeader("Content-Disposition", "attachment;filename="+ filename); res ...