MVC [Control与View交互]2

2015-07-20 17:33:36 · 作者: · 浏览: 5

<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
  
   

@{
    Layout = null;
}

html>



    
   
    
   Index


    
   
@foreach (var item in Model) { }
编号 姓名 年龄
@item.Id @item.Name @item.Age @Html.ActionLink("删除", "Delete", new { id=item.Id})


Delete (删除信息页)

@model MvcApplication1.Models.T_User

@{
    Layout = null;
}





    
  
    Delete


    

你确定要删除这条记录吗?

T_User
编号: @Model.Id
姓名: @Model.Name
年龄: @Model.Age
@using (Html.BeginForm()) {

| @Html.ActionLink("跳转到首页", "Index")

}
\ \