当前位置:Gxlcms > mysql > mvc3.0+linq操作数据库中表的数据(ps:本人菜鸟刚学)

mvc3.0+linq操作数据库中表的数据(ps:本人菜鸟刚学)

时间:2021-07-01 10:21:17 帮助过:25人阅读

1:添加控制器类文件HomeController.cs其代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcTestData.Models; namespace MvcTestData.Controllers{ public class HomeContr

1:添加控制器类文件HomeController.cs其代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcTestData.Models;
namespace MvcTestData.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            TestDataContext txtData = new TestDataContext();
            var result=from info in txtData.StuTable
                       select info;
            ViewData["data"] = result;
            return View(result);
        }

        public ActionResult Add(FormCollection form)
        {
            string id =form["StuId"];
            string name=form["StuName"];
            string sex = form["StuSex"];
            int age = int.Parse(form["StuAge"]);
            string address = form["StuAddress"];

            StuTable stu = new StuTable();
            stu.StuId = id;
            stu.StuName = name;
            stu.StuSex = sex;
            stu.StuAge = age;
            stu.StuAddress = address;

            try
            {
                using (var db = new TestDataContext())
                {
                    db.StuTable.InsertOnSubmit(stu);
                    db.SubmitChanges();
                    ViewData["result"] = "ok";
                }
            }
            catch 
            {
                ViewData["result"] = "fail";
                throw;
            }
            return View("Add");
        }

        public ViewResult AddInfo()
        {
            return View("AddInfo");
        }

        public ViewResult Delete()
        {
            int id = Int16.Parse(Request.Form["id"]);
            try
            {
                using (var db = new TestDataContext())
                {
                    db.StuTable.DeleteOnSubmit(db.StuTable.First(info => info.ID == id));
                    db.SubmitChanges();
                    ViewData["result"] = "ok";
                }
            }
            catch
            {
                ViewData["result"] = "fail";
                throw;
            }
            return View("Delete");
        }

       

    }
}

View Code

2:为models文件夹添加linq to sql 类文件然后把数据库中的表copy 进来


3:为控制器中的Action添加各自的视图

4 视图Index.cshtml的代码

@using MvcTestData.Models



    Index


    
"0" cellspacing="0" cellpadding="0" width="100%" style="text-align:center" > @foreach (StuTable info in (ViewData["data"] as IEnumerable)) { }
序号学号姓名性别年龄住址操作
@info.ID @info.StuId @info.StuName @info.StuSex @info.StuAge @info.StuAddress

@Html.ActionLink("添加个人信息","AddInfo","Home")

View Code

5 视图 Add.cshtml的代码

@model MvcTestData.Models.StuTable

@{
    ViewBag.Title = "Add";
}
@if(ViewData["result"].Equals("ok"))
{
    

添加成功

} else {

添加失败

}

View Code

6 视图AddInfo.cshtml代码

@model MvcTestData.Models.StuTable

@{
    ViewBag.Title = "AddInfo";
}

AddInfo

@using(Html.BeginForm("Add","Home",FormMethod.Post)) {

Student 学号:@Html.TextBoxFor(x=>x.StuId)

Student 姓名:@Html.TextBoxFor(x=>x.StuName)

Student 性别:@Html.TextBoxFor(x=>x.StuSex)

Student 年龄:@Html.TextBoxFor(x=>x.StuAge)

Student 住址:@Html.TextBoxFor(x=>x.StuAddress)

"submit" value="Add" /> }

View Code

7 视图Delete.cshtml代码

@model MvcTestData.Models.StuTable

@{
    ViewBag.Title = "Delete";
}

@if (ViewData["result"].Equals("ok"))
{
    

删除成功

} else {

删除失败

}

View Code

8 最终测试结果图:

人气教程排行