标签:
MVC HtmlHelper;
1.Url():
<%= Html.ActionLink(“用户列表","方法","控制器") %>
2.HtmlHelper控件
<%= Html.TextBox("username", null, null, new { placeholder="在此输入用户名"})%>
强类型转换
1.将ViewData转换为强类型
<%IEnumerable<tb_ABc> list = ViewData["data"] as IEnumerable<tb_ABc>;%>
2.将页面第一行的ViewPage<dynamic>修改成强类型ViewPage<MvcUserDemo.Models.UserInfo> 。即可直接在页面中通过Model调
用Action中传递过来的对象。
3.MVC自己提供的强类型转换自动生成视图(建议)
在创建视图时给创建强类型视图勾上选择模类型,支架模板即可。使用强类型视图可以自动生成页面
强类型视图对表的增删查改:
查:
*.cs代码:
UserDBDataContext dc = new UserDBDataContext();//Linq
//
// GET: /InfoUser/
public ActionResult Index()
{
ViewData["data"] = dc.tb_ABc.AsEnumerable<tb_ABc>();
return View();
}
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(tb_ABc tb)
{
dc.tb_ABc.InsertOnSubmit(tb);
dc.SubmitChanges();
return RedirectToAction("Index");
}
[HttpGet]
public ActionResult Edit(int? id)
{
ViewData.Model = dc.tb_ABc.Where(x => x.Sid == id).First();
dc.SubmitChanges();
return View();
}
[HttpPost]
public ActionResult Edit(tb_ABc tb)
{
var Reulst = from u in dc.tb_ABc
where u.Sid == tb.Sid
select u;
foreach (var item in Reulst)
{
item.sname = tb.sname;
item.Area_ID = tb.Area_ID;
}
dc.SubmitChanges();
return RedirectToAction("Index");
}
public ActionResult Delete(int? Id)
{
dc.tb_ABc.DeleteOnSubmit(dc.tb_ABc.Where(x => x.Sid == Id).First());
dc.SubmitChanges();
return RedirectToAction("Index");
}
------
*.aspx代码:
<%@ Import Namespace="MvcDeom.Models" %>//添加引用
<%IEnumerable<tb_ABc> list = ViewData["data"] as IEnumerable<tb_ABc>;%>
<div>
<table class="auto-style1">
<tr>
<td>编号</td>
<td>用户</td>
<td>密码</td>
<td>详细</td>
<td>删除</td>
</tr>
<%foreach (var item in list)
{
%>
<tr>
<td><%= Html.ActionLink(item.Sid.ToString(),"Edit","InfoUser",new {
id=item.Sid},null)%></td>
<td><%= item.sname %></td>
<td><%= item.Area_ID %></td>
<td><%= Html.ActionLink("详细", "Detail", new {Id = item.Sid})%></td>
<td><%= Html.ActionLink("删除", "Delete", new {Id = item.Sid})%></td>
</tr>
<%
}
%>
</table>
标签:
原文地址:http://www.cnblogs.com/wangxlei/p/5982760.html