在Model中连接数据库
此程序为在index页面中显示S表的内容,AddView页面可向S表中添加内容
DefaultController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zsy.Models; //注意引用命名空间
namespace zsy.Controllers
{
public class DefaultController : Controller
{
SEntities1 db = new SEntities1();
// GET: Default
public ActionResult Index()
{
//查询表中所有的记录
List<S> list = db.S.ToList();
//将数据传递到前端页面
ViewBag.list = list;
return View();
}
public ActionResult AddView()
{
return View();
}