Top
db.UserInfo.Where(u=>u.Id==1).Select(c=>new{Name=c.UserName,Pwd=c.UserPwd});
<%=Html.ActionLink("首页","Index")%>
点击跳转
//等号输出=渲染Html标签
<%=Html.ActionLink("首页","Index")%>
//冒号输出:Server.HtmlEncode 操作 (纯文本输出)
//可避免XSS攻击
<%:Html.ActionLink("首页","Index")%>
<%using(Ajax.BeginForm("Index",new AjaxOptions{Confirm="确定?",InsertionMode=System.Web.Mvc.Ajax.InsertionMode.Replace,UpdateTargetId="div1"}))%>
HtmlHelper的扩展:
注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束。
//主要就是输出分页的超级链接的标签
//自定义分页Helper扩展
public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
{
var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == 0 ? 3 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
var output = new StringBuilder();
if (totalPages > 1)
{
//if (currentPage != 1)
{//处理首页连接
output.AppendFormat("首页 ", redirectTo, pageSize);
}
if (currentPage > 1)
{//处理上一页的连接
output.AppendFormat("上一页 ", redirectTo, currentPage - 1, pageSize);
}
else
{
// output.Append("上一页");
}
output.Append(" ");
int currint = 5;
for (int i = 0; i <= 10; i++)
{//一共最多显示10个页码,前面5个,后面5个
if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
{
if (currint == i)
{//当前页处理
//output.Append(string.Format("[{0}]", currentPage));
output.AppendFormat("{3} ", redirectTo, currentPage , pageSize, currentPage );
}
else
{//一般页处理
output.AppendFormat("{3} ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
}
}
output.Append(" ");
}
if (currentPage < totalPages)
{//处理下一页的链接
output.AppendFormat("下一页 ", redirectTo, currentPage + 1, pageSize);
}
else
{
//output.Append("下一页");
}
output.Append(" ");
if (currentPage != totalPages)
{
output.AppendFormat("末页 ", redirectTo, totalPages, pageSize);
}
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
return new HtmlString(output.ToString());
}
//样式部分:
body
{
}
.paginator
{
font: 12px Arial, Helvetica, sans-serif;
padding: 10px 20px 10px 0;
margin: 0px;
}
.paginator a
{
border: solid 1px #ccc;
color: #0063dc;
cursor: pointer;
text-decoration: none;
}
.paginator a:visited
{
padding: 1px 6px;
border: solid 1px #ddd;
background: #fff;
text-decoration: none;
}
.paginator .cpb
{
border: 1px solid #F50;
font-weight: 700;
color: #F50;
background-color: #ffeee5;
}
.paginator a:hover
{
border: solid 1px #F50;
color: #f60;
text-decoration: none;
}
.paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover
{
float: left;
height: 16px;
line-height: 16px;
min-width: 10px;
_width: 10px;
margin-right: 5px;
text-align: center;
white-space: nowrap;
font-size: 12px;
font-family: Arial,SimSun;
padding: 0 3px;
}
点击跳转Top
public class DBSession:IDBSession
{
DbContext db = new ReportServerEntities();
private Users _usersDAL;
public Users UsersDAL
{
get
{
if (_usersDAL == null)
{
_usersDAL = AbstractFactory.CreateUsers();//抽象工程
}
return _usersDAL;
}
set { _usersDAL = value; }
}
///
/// 执行特殊sql语句
///
///
///
///
public int ExecuteSql(string sql, params SqlParameter pars)
{
return db.Database.ExecuteSqlCommand(sql, pars);
}
public int SaveChanges()
{
return db.SaveChanges();
}
}
点击跳转
public class DbContextFactory
{
public static DbContext GetCurrentDbContext()
{
DbContext dbContext = (DbContext)CallContext.GetData("DbContext");
if (dbContext==null)
{
dbContext = new ReportServerEntities();
CallContext.SetData("DbContext", dbContext);
}
return dbContext;
}
}
点击跳转
//默认直接获取 IEnumerable
IQueryable LoadEntities(Funcbool> whereLambda);
//获取 IQueryable
IQueryable LoadEntities(System.Linq.Expressions.Expressionbool>> whereLambda);
点击跳转