006. MVC的第一个增删改查弱类型和强类型
项目图:

弱类型的前台主要代码
@{
Layout = null;
}
@*使用model要添加对其的引用*@
@model MvcCRUDDemo.OrderInfo
<!DOCTYPE html>
@*@inherits System.Web.Mvc.ViewPage<OrderInfo>*@
@*<%@ page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcCRUDDemo.Model.OrderInfo>%>*@
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
</head>
<body>
<div>
@{
var ordreInfo = ViewData["OrderInfo"] as OrderInfo;
//var orderInfo1 = ViewData.Model.Id;
}
访问此页面的时候要使用id=1,这样的形式访问, 比如<br />
http://localhost:10463/OrderInfo/ShowOrderInfo?id=1 <br />
或者<br />
http://localhost:10463/OrderInfo/ShowOrderInfo/1 这种方式需要路由支持<br />
以上两种传值方式都可以使用, 其中比较推荐第二种, 注意第二种斜线1, 这种是由路由决定的, 路由里面有个{id}, 这里也必须是对id进行传递值
@*<table border="1">
<tr><td>编号</td><td>书名</td><td>作者</td></tr>
<tr><td>@(ordreInfo.Id)</td><td>@(ordreInfo.BookName)</td><td>@(ordreInfo.BookAuth)</td></tr>
</table>*@
我是弱类型表格
<table border="1">
<tr><td>编号</td><td>@(ordreInfo.Id)</td></tr>
<tr><td>书名</td><td>@(ordreInfo.BookName)</td></tr>
<tr><td>作者</td><td>@(ordreInfo.BookAuth)</td></tr>
</table>
我是强类型出来的表格<br />
在aspx中, 使用Model是需要添加继承的<br />
//如果后台返回List<ordreInfo>那么前台这样的方式就不能直接使用了, 需要循环了
@*<table border="1">
<tr><td>编号</td><td>@(Model.Id)</td></tr>
<tr><td>书名</td><td>@(Model.BookName)</td></tr>
<tr><td>作者</td><td>@(Model.BookAuth)</td></tr>
</table>*@
<br />
@(Html.TextBoxFor<OrderInfo,string >(order=>order.BookName))
这里需要添加引@model MvcCRUDDemo.OrderInfo <br />
<br/>
这里有个For就表示要出入Lambda表达式, 如果不加For表示只需要传入一个字符串即可
<br />这是一个强类型出来的TextBox, 单个列表
<br /> 自动赋值必须使用htmlHelper标签才可以, 手写的是不行的<br />
@(Html.TextBox("Id"))<br />
@(Html.TextBox("BookAuth"))
<br />即使使用list, 它这里也可以取到对应的值
</div>
</body>
</html>
强类型主要代码, 前台生成的代码基本没有动过
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcCRUDDemo.Controllers
{
public class OrderInfoController : Controller
{
// GET: OrderInfo
MyGroupDBEntities dbcontext = new MyGroupDBEntities();
public ActionResult Index()
{
//指定一个Model
ViewData.Model= dbcontext.OrderInfo.AsEnumerable();
return View();
}
// GET: OrderInfo/Details/5
public ActionResult Details(int id)
{
ViewData.Model = dbcontext.OrderInfo.Find(id);//推荐使用这一种
//ViewData.Model = dbcontext.OrderInfo.Where<OrderInfo>(o => o.Id == id).FirstOrDefault();
return View();
}
// GET: OrderInfo/Create
public ActionResult Create()
{
return View();
}
// POST: OrderInfo/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
if (collection.Count > 0)
{
OrderInfo orderInfo = dbcontext.OrderInfo.Find(collection["id"]);
dbcontext.OrderInfo.Add(orderInfo);
dbcontext.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: OrderInfo/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: OrderInfo/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
OrderInfo orderInfo = dbcontext.OrderInfo.Find(id);
orderInfo.BookAuth = collection["BookAuth"];
orderInfo.BookName = collection["BookName"];
//orderInfo.UserInfo_Id =int.Parse(collection["UserInfo_Id"]);
dbcontext.OrderInfo.Add(orderInfo);
dbcontext.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception e)
{
//s return Content(e.ToString());
return View();
}
}
// GET: OrderInfo/Delete/5
public ActionResult Delete(int id)
{
ViewData.Model = dbcontext.OrderInfo.Find(id);
return View();
}
// POST: OrderInfo/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
OrderInfo orderInfo = dbcontext.OrderInfo.Find(id);
dbcontext.Entry(orderInfo).State = System.Data.Entity.EntityState.Deleted;
dbcontext.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
浙公网安备 33010602011771号