如何通过MVC向数据库中添加数据?用 的是。net、C#、linq to sql 类,在线等!!! 5
4个回答
展开全部
上学期教同学时写了一个绝对适合入门的小例子 用的是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>
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>
展开全部
ASP.NET MVC3.0是一个页面展示层的框架,将我们平日理解的三层架构(页面展示层,业务逻辑层,数据访问层)的页面展示层,再分为mvc三层。其中的m表示业务模型,v标识展示的html,c是页面展示的控制器,负责页面逻辑哦。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
现在最佳的选择还是实体框架也就是Linq to Entity ........
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
写 链接数据库的 字符串 了~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询