实现qq邮箱换肤(第一季 )
本文代码要实现的目标:显示可供用户选择的风格模版,并允许其更新选择。
简单流程:
1、从数据库获取风格模版的列表。
2、获取一个用户所选择的风格模版,若该用户从未选择风格模版,那么使用默认选中项
3、用户更改风格模版后保存。
使用的技术:三层架构,mvc,linq,jquery。
废话不多说,直接上图片:
之前的风格:
选择后的风格:
局部视图(partialView)
代码
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<BL.Style>>" %>
<%
SelectList list=null;
if (ViewData["selectList"] != null) {
list = ViewData["selectList"] as SelectList;
}
%>
<div class="itemcontent-font btnHyperLink" style=" text-align:center; width:50%;" >
请选择样式模版:
<%=Html.DropDownList("aa", list)%>
<br />
<%=
Html.ActionLink("取 消", null, null, new { href = "javascript:void(0)", onclick = "$('div#workArea').css('display','none');" })
%>|
<%=
Html.ActionLink("保 存", null, null, new { href = "javascript:void(0)", onclick = "return submitStyle();" })
%>
</div>
<%
SelectList list=null;
if (ViewData["selectList"] != null) {
list = ViewData["selectList"] as SelectList;
}
%>
<div class="itemcontent-font btnHyperLink" style=" text-align:center; width:50%;" >
请选择样式模版:
<%=Html.DropDownList("aa", list)%>
<br />
<%=
Html.ActionLink("取 消", null, null, new { href = "javascript:void(0)", onclick = "$('div#workArea').css('display','none');" })
%>|
<%=
Html.ActionLink("保 存", null, null, new { href = "javascript:void(0)", onclick = "return submitStyle();" })
%>
</div>
点击“保存”触发的javascript脚本
//提交个人风格模版
function submitStyle() {
var styleID = $('#aa').get(0).value;
window.location.href=。。/../SiteStyle/SubmitStyle?styleID=+ styleID
return false;
}
function submitStyle() {
var styleID = $('#aa').get(0).value;
window.location.href=。。/../SiteStyle/SubmitStyle?styleID=+ styleID
return false;
}
Controller中的代码:
代码
/// <summary>
/// 可供选择的风格模版列表,用户如选择了风格模版,那么StyleID作为下拉框的被选择项;
/// 如果用户没有选择风格模版,那么选中项默认为StyleID==1的项
/// </summary>
/// <returns></returns>
public ActionResult StyleCanSelected(int typeID)
{
Guid authorGuid = Helpers.Helper.GetVisitor().UserGuid;
//获取样式模版列表
List<BL.Style> styles = obj.GetStylesByType(typeID);
//获取用户选中的样式模版
BL.Style style = obj.GetStyleByUser(authorGuid);
//用户未选择样式模版时,默认StyleID == 1的模版
if (style == null)
{
style = new BL.Style() { StyleID = 1 };
}
//用户绑定dropdownlist的数据,styles为业务逻辑的数据,
//StyleID为value字段,StyleDescription为text字段,style.StyleID为选中项的value值
SelectList selectList = new SelectList(styles, "StyleID", "StyleDescription",style.StyleID);
ViewData["selectList"] = selectList;
return View(styles);
}
/// <summary>
/// 提交用户的选择,保存到数据库
/// </summary>
/// <param name="styleID"></param>
/// <returns></returns>
public ActionResult SubmitStyle(int styleID)
{
Guid authorGuid = Helpers.Helper.GetVisitor().UserGuid;
obj.SetupStyle(authorGuid, styleID);
//刷新设置页面
return RedirectToAction("PersonSetting", "Person", new { ownerGuid = authorGuid });
}
/// 可供选择的风格模版列表,用户如选择了风格模版,那么StyleID作为下拉框的被选择项;
/// 如果用户没有选择风格模版,那么选中项默认为StyleID==1的项
/// </summary>
/// <returns></returns>
public ActionResult StyleCanSelected(int typeID)
{
Guid authorGuid = Helpers.Helper.GetVisitor().UserGuid;
//获取样式模版列表
List<BL.Style> styles = obj.GetStylesByType(typeID);
//获取用户选中的样式模版
BL.Style style = obj.GetStyleByUser(authorGuid);
//用户未选择样式模版时,默认StyleID == 1的模版
if (style == null)
{
style = new BL.Style() { StyleID = 1 };
}
//用户绑定dropdownlist的数据,styles为业务逻辑的数据,
//StyleID为value字段,StyleDescription为text字段,style.StyleID为选中项的value值
SelectList selectList = new SelectList(styles, "StyleID", "StyleDescription",style.StyleID);
ViewData["selectList"] = selectList;
return View(styles);
}
/// <summary>
/// 提交用户的选择,保存到数据库
/// </summary>
/// <param name="styleID"></param>
/// <returns></returns>
public ActionResult SubmitStyle(int styleID)
{
Guid authorGuid = Helpers.Helper.GetVisitor().UserGuid;
obj.SetupStyle(authorGuid, styleID);
//刷新设置页面
return RedirectToAction("PersonSetting", "Person", new { ownerGuid = authorGuid });
}
业务逻辑层(BLL)中代码:
代码
public class CustomStyleBL
{
CustomStyleRep obj = new CustomStyleRep();
/// <summary>
/// 获取一种类型的风格模版列表
/// </summary>
/// <param name="typeID"></param>
/// <returns></returns>
public List<Style> GetStylesByType(int typeID)
{
return obj.GetStylesByType(typeID).ToList();
}
/// <summary>
/// 设置用户风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <param name="styleID"></param>
/// <returns></returns>
public int SetupStyle(Guid userGuid, int styleID)
{
return obj.SetupStyle(userGuid, styleID);
}
/// <summary>
/// 获取一人的风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <returns></returns>
public Style GetStyleByUser(Guid userGuid)
{
return obj.GetStyleByUser(userGuid);
}
}
{
CustomStyleRep obj = new CustomStyleRep();
/// <summary>
/// 获取一种类型的风格模版列表
/// </summary>
/// <param name="typeID"></param>
/// <returns></returns>
public List<Style> GetStylesByType(int typeID)
{
return obj.GetStylesByType(typeID).ToList();
}
/// <summary>
/// 设置用户风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <param name="styleID"></param>
/// <returns></returns>
public int SetupStyle(Guid userGuid, int styleID)
{
return obj.SetupStyle(userGuid, styleID);
}
/// <summary>
/// 获取一人的风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <returns></returns>
public Style GetStyleByUser(Guid userGuid)
{
return obj.GetStyleByUser(userGuid);
}
}
数据访问层(DAL)中代码:
代码
public class CustomStyleRep
{
EschoolBLDataContext db = new EschoolBLDataContext();
/// <summary>
/// 获取指定类别的风格模版
/// </summary>
/// <param name="typeID"></param>
/// <returns></returns>
public IQueryable<Style> GetStylesByType(int typeID)
{
IQueryable<Style> query = db.Style.Where(u => u.TypeID == typeID);
return query;
}
/// <summary>
/// 设置风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <param name="styleID"></param>
/// <returns></returns>
public int SetupStyle(Guid userGuid,int styleID)
{
var userStyle = db.UserStyle.SingleOrDefault(u => u.UserGuid == userGuid);
if (userStyle != null)
{
//更新风格模版
userStyle.Style = db.Style.SingleOrDefault(u => u.StyleID == styleID);
}
else
{
//添加风格模版
userStyle = new UserStyle
{
UserGuid = userGuid,
StyleID = styleID,
ModifyTime = DateTime.Now
};
db.UserStyle.InsertOnSubmit(userStyle);
}
int count = db.GetChangeSet().Inserts.Count + db.GetChangeSet().Updates.Count;
db.SubmitChanges();
return count;
}
/// <summary>
/// 获取用户的风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <returns></returns>
public Style GetStyleByUser(Guid userGuid)
{
var userStyle = db.UserStyle.SingleOrDefault(u => u.UserGuid == userGuid);
if(userStyle!=null)
{
return userStyle.Style;
}else
{
return null;
}
}
}
{
EschoolBLDataContext db = new EschoolBLDataContext();
/// <summary>
/// 获取指定类别的风格模版
/// </summary>
/// <param name="typeID"></param>
/// <returns></returns>
public IQueryable<Style> GetStylesByType(int typeID)
{
IQueryable<Style> query = db.Style.Where(u => u.TypeID == typeID);
return query;
}
/// <summary>
/// 设置风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <param name="styleID"></param>
/// <returns></returns>
public int SetupStyle(Guid userGuid,int styleID)
{
var userStyle = db.UserStyle.SingleOrDefault(u => u.UserGuid == userGuid);
if (userStyle != null)
{
//更新风格模版
userStyle.Style = db.Style.SingleOrDefault(u => u.StyleID == styleID);
}
else
{
//添加风格模版
userStyle = new UserStyle
{
UserGuid = userGuid,
StyleID = styleID,
ModifyTime = DateTime.Now
};
db.UserStyle.InsertOnSubmit(userStyle);
}
int count = db.GetChangeSet().Inserts.Count + db.GetChangeSet().Updates.Count;
db.SubmitChanges();
return count;
}
/// <summary>
/// 获取用户的风格模版
/// </summary>
/// <param name="userGuid"></param>
/// <returns></returns>
public Style GetStyleByUser(Guid userGuid)
{
var userStyle = db.UserStyle.SingleOrDefault(u => u.UserGuid == userGuid);
if(userStyle!=null)
{
return userStyle.Style;
}else
{
return null;
}
}
}