如何通过MVC向数据库中添加数据?用 的是。net、C#、linq to sql 类,在线等!!!

如何通过MVC向数据库中添加数据?用 的是。net、C#、linq to sql 类,在线等!!!,第1张

上学期教同学时写了一个绝对适合入门的小例子 用的是asp.net mvc3+linq to entity

linq to entity和linq to sql在使用上本身差距不大 你肯定能看懂的

*razor页面中用的是弱类型的helper

还有疑问可以去此论坛.net区提问:

http://bbs.ctrlc.com.cn/

欢迎交流

原文地址:http://user.qzone.qq.com/523080653/blog/1300030445

--------------------------------------------------------------------------------------------------------------

今天给同学演示用的demo 无美工无分层无XXXXXX 三无产品 绝对适合入门...

就一张表 UserInfo{UserID,UserName,UserPassword}

逻辑写controller里了 model里就一个实体 就不列出来了

HomeController

using System

using System.Collections.Generic

using System.Linq

using System.Web

using System.Web.Mvc

using TestDemo.Models

using System.Data.Objects

namespace TestDemo.Controllers

{

public class HomeController : Controller

{

//

// GET: /Home/

public ActionResult Index()

{

TestEntities te = new TestEntities()

ObjectQuery<UserInfo>userInfos = te.UserInfoes

var users = from u in userInfos

select u

ViewData.Model = users.ToList()

return View()

}

public ActionResult Delete(int id)

{

TestEntities te = new TestEntities()

ObjectQuery<UserInfo>userInfos = te.UserInfoes

var query = userInfos.Single(u =>u.UserID == id)

te.DeleteObject(query)

te.SaveChanges()

return RedirectToAction("Index")

}

public ActionResult Add()

{

return View()

}

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Add(FormCollection form)

{

TestEntities te = new TestEntities()

UserInfo addUser = new UserInfo()

{

UserName = form["userName"].ToString(),

UserPassword = form["userPassword"].ToString()

}

te.UserInfoes.AddObject(addUser)

te.SaveChanges()

return RedirectToAction("Index")

}

public ActionResult Edit(int id)

{

TestEntities te = new TestEntities()

ObjectQuery<UserInfo>userInfos = te.UserInfoes

UserInfo user = userInfos.Single(u =>u.UserID == id)

ViewData.Model = user

return View()

}

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Edit(FormCollection form)

{

TestEntities te = new TestEntities()

ObjectQuery<UserInfo>userInfos = te.UserInfoes

int id = Int32.Parse( form["userID"].ToString())

var query = from u in userInfos

where u.UserID == id

select u

UserInfo editUser = query.First()

UserInfo user = new UserInfo()

{

UserID=id,

UserName = form["userName"].ToString(),

UserPassword = form["userPassword"].ToString()

}

editUser.UserName = user.UserName

editUser.UserPassword = user.UserPassword

te.SaveChanges()

return RedirectToAction("Index")

}

}

}

Add.cshtml

@{

ViewBag.Title = "Add"

}

<h2>Add</h2>

@using (Html.BeginForm())

{

@Html.Label("姓名")

@Html.TextBox("userName")

<hr />

@Html.Label("密码")

@Html.TextBox("userPassword")

<input type="submit" value="提交"/>

}

Edit.cshtml

@using TestDemo.Models

@{

ViewBag.Title = "Edit"

}

<h2>

Edit</h2>

@using (Html.BeginForm())

{

UserInfo query = ViewData.Model

@Html.Label("ID", query.UserID.ToString())

<br />

@Html.Label("姓名")

@Html.TextBox("userName")<br />

@Html.Label("密码")

@Html.TextBox("userPassword")<br />

@Html.Hidden("userID", query.UserID)

<input type="submit" value="提交" />

}

Index.cshtml

@using TestDemo.Models

@{

ViewBag.Title = "Index"

}

<!DocType html />

<html>

<head>

<title></title>

<link href="@Url.Content("../Content/Test.css")" type="text/css" rel="Stylesheet" />

</head>

<body>

<div class="all" >

<div class="top">

</div>

<div class="content">

@{

List<UserInfo>users = ViewData.Model

@Html.ActionLink("添加", "Add")

<table id="qzw">

@foreach (UserInfo user in users)

{

<tr>

<td>@user.UserID

</td>

<td>@user.UserName

</td>

<td>@user.UserPassword

</td>

<td>@Html.ActionLink("删除", "Delete", new { id = user.UserID })

</td>

<td>@Html.ActionLink("修改", "Edit", new { id = user.UserID })

</td>

</tr>

}

</table>

}

</div>

</div>

</body>

</html>

你是想问原理,还是只想学 *** 作

原理:就是利用反射,进行数据库 *** 作;在你自动生成model时,他自己会维护一个关系;自动插入。

*** 作,就是利用源代码生成器,自动生成,直接用就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6776225.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存