首页 > 代码库 > MVC [Control与View交互]2
MVC [Control与View交互]2
<1>
控制器 Home
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class HomeController : Controller { private salesEntities db = new salesEntities(); // // GET: /Home/ public ActionResult Index() { //ViewData.Model = db.T_User.ToList(); //return View(); //以上两条代码相当于 return View(db.T_User.ToList()); //括号里的db.T_User.ToList()其实就是一个Model } public ActionResult Delete(int id) { //Single:返回序列中满足指定条件的唯一元素;如果有多个这样的元素存在,则会引发异常 T_User t_user = db.T_User.Single(t => t.Id == id); return View(t_user); } } }
视图 Index (首页)
@*@model MvcApplication1.Models.T_User*@ @model IEnumerable<MvcApplication1.Models.T_User> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <table border="1"> <tr><th>编号</th><th>姓名</th><th>年龄</th></tr> @foreach (var item in Model) { <tr> <td>@item.Id</td><td>@item.Name</td><td>@item.Age</td> <td>@Html.ActionLink("删除", "Delete", new { id=item.Id})</td> <td></td> </tr> } </table> </div> </body> </html>
Delete (删除信息页)
@model MvcApplication1.Models.T_User @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Delete</title> </head> <body> <h3>你确定要删除这条记录吗?</h3> <fieldset> <legend>T_User</legend> <table> <tr><th>编号:</th><th>@Model.Id</th></tr> <tr><th>姓名:</th><th>@Model.Name</th></tr> <tr><th>年龄:</th><th>@Model.Age</th></tr> </table> </fieldset> @using (Html.BeginForm()) { <p> <input type="submit" value=http://www.mamicode.com/"Delete" /> |>MVC [Control与View交互]2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。