MVC4做网站六后台管理:6.2网站信息设置_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > MVC4做网站六后台管理:6.2网站信息设置

MVC4做网站六后台管理:6.2网站信息设置

 2013/11/13 2:45:25  洞庭夕照  博客园  我要评论(0)
  • 摘要:用来实现网站标题、名称、关键字、描述、版权等信息的设置。模型字段:网站的设置信息前后台都要用到,所以要把模型方式Ninesky/Models文件夹中,代码如下://////////////////////网站设置模型//创建日期2013-8-1//修改日期2013-8-4///////////////////usingSystem.ComponentModel.DataAnnotations;namespaceNinesky.Models{///<summary>
  • 标签:MVC 网站

用来实现网站标题、名称、关键字、描述、版权等信息的设置。

模型字段:

image

网站的设置信息前后台都要用到,所以要把模型方式Ninesky/Models文件夹中,代码如下:

////////////////////
//网站设置模型
//创建日期2013-8-1
//修改日期2013-8-4
///////////////////

using System.ComponentModel.DataAnnotations;

namespace Ninesky.Models
{
    /// <summary>
    /// 网站信息设置
    /// </summary>
    public class SiteConfig
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 网站名称
        /// </summary>
        [Required(ErrorMessage="必须输入网站名称!")]
        [StringLength(50,MinimumLength=4, ErrorMessage="必须是4-50个字符!")]
        [Display(Name="网站名称",Description="必填,4-50个字符。")]
        public string Name { get; set; }
        /// <summary>
        /// 网站标题
        /// </summary>
        [Required(ErrorMessage = "必须输入网站标题!")]
        [StringLength(50, MinimumLength = 4, ErrorMessage = "必须是4-50个字符!")]
        [Display(Name = "网站标题", Description = "必填,4-50个字符。")]
        public string Title { get; set; }
        /// <summary>
        /// 网站地址
        /// </summary>
        [Required(ErrorMessage = "必须输入网站地址!")]
        [StringLength(50, MinimumLength = 4, ErrorMessage = "必须是4-50个字符!")]
        [Display(Name = "网站地址", Description = "必填,4-50个字符。")]
        public string Url { get; set; }
        /// <summary>
        /// Logo地址
        /// </summary>
        [StringLength(255, ErrorMessage = "必须少于255个字符!")]
        [Display(Name = "Logo地址", Description = "小于255个字符。")]
        public string LogoUrl { get; set; }
        /// <summary>
        /// Meta描述语
        /// </summary>
        [Required(ErrorMessage = "必须输入Meta描述语!")]
        [StringLength(500, ErrorMessage = "描述语之间用“,”隔开,必须少于500个字符!")]
        [Display(Name = "Meta描述语", Description = "小于500个字符。")]
        [DataType(DataType.MultilineText)]
        public string MetaDescription { get; set; }
        /// <summary>
        /// Meta关键字
        /// </summary>
        [Required(ErrorMessage = "必须输入Meta关键字!")]
        [StringLength(500, ErrorMessage = "关键字之间用“,”隔开,必须少于500个字符!")]
        [Display(Name = "Meta关键字", Description = "小于500个字符。")]
        [DataType(DataType.MultilineText)]
        public string MetaKeywords { get; set; }
        /// <summary>
        /// 版权信息
        /// </summary>
        [Required(ErrorMessage = "必须输入版权!")]
        [StringLength(500, ErrorMessage = "必须少于500个字符!")]
        [Display(Name = "版权信息", Description = "支持Html,小于500个字符。")]
        [DataType(DataType.MultilineText)]
        public string Copyright { get; set; }
    }
}

同样在Ninesky/Repository文件夹中添加接口InterfaceSiteConfig

using Ninesky.Models;

namespace Ninesky.Repository
{
    /// <summary>
    /// 网站设置信息接口
    /// <remarks>
    /// 版本v1.0
    /// 创建2013.8.4
    /// </remarks>
    /// </summary>
    public interface InterfaceSiteConfig
    {
        /// <summary>
        /// 查找设置
        /// </summary>
        /// <returns></returns>
        SiteConfig Find();

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="siteConfig">设置</param>
        /// <returns></returns>
        bool Save(SiteConfig siteConfig);
    }
}

再添加SiteConfigRepository.cs

using Ninesky.Models;
using System.Linq;

namespace Ninesky.Repository
{
    /// <summary>
    /// <remarks>
    /// 版本v1.0
    /// 创建2013.8.4
    /// </remarks>
    /// </summary>
    public class SiteConfigRepository:InterfaceSiteConfig
    {
        private NineskyContext nineskyContext;
        /// <summary>
        /// 查找设置
        /// </summary>
        /// <returns></returns>
        public SiteConfig Find()
        {
            using (nineskyContext = new NineskyContext())
            {
                return nineskyContext.SiteConfig.SingleOrDefault();
            }
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="siteConfig">设置</param>
        /// <returns></returns>
        public bool Save(SiteConfig siteConfig)
        {
            using (nineskyContext = new NineskyContext())
            {
                if (nineskyContext.SiteConfig.Count() == 0) nineskyContext.SiteConfig.Add(siteConfig);
                else
                {
                    nineskyContext.SiteConfig.Attach(siteConfig);
                    nineskyContext.Entry<SiteConfig>(siteConfig).State = System.Data.EntityState.Modified;
                }
                return nineskyContext.SaveChanges() > 0;
            }
        }
    }
}

后台部分:

在~/Areas/Admin/Controllers中添加SystemController.cs

添加局部视图action public PartialViewResult Config() 及保存处理的action public JsonResult Config(SiteConfig siteConfig)

/// <summary>
        /// 基本信息设置
        /// </summary>
        /// <returns></returns>
        public PartialViewResult Config()
        {
            var _siteConfig = new SiteConfigRepository().Find();
            if (_siteConfig == null) _siteConfig = new SiteConfig() { Id = 0, Name = "NineSky", Title = "欢迎光临NineSky!" };
            return PartialView(_siteConfig);
        }

        /// <summary>
        /// Config保存
        /// </summary>
        /// <param name="siteConfig"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Config(SiteConfig siteConfig)
        {
            JsonData _jdata = new JsonData();
            if (ModelState.IsValid)
            {
                var _scRsy = new SiteConfigRepository();
                if (_scRsy.Save(siteConfig))
                {
                    _jdata.Success = true;
                    _jdata.Message = ("保存成功√");
                }
                else
                {
                    _jdata.Success = false;
                    _jdata.Message = ("保存数据时发生错误");
                }
            }
            else
            {
                _jdata.Success = false;
                var _eItem = ModelState.Where(m => m.Value.Errors.Count > 0);
                foreach (var i in _eItem)
                {
                    _jdata.MessageLsit.Add(i.Key, "验证失败!");
                }
                _jdata.Message = ("保存数据时发生错误");

            }
            return Json(_jdata);
        }

为action 添加视图

@model Ninesky.Models.SiteConfig

<div class="c_navbar">网站设置 >> 基本信息</div>

@using (Html.BeginForm("Config", "System", FormMethod.Post, new { id = "siteconfig_form" }))
{
    @Html.AntiForgeryToken()
    <div class="fs_wapper">
        <div class="header">网站设置 @Html.HiddenFor(model => model.Id)</div>
        @Html.ValidationSummary()
        <table>
            <tr>
                <th>@Html.LabelFor(model => model.Name)</th>
                <td>@Html.EasyuiInput(model=>model.Name,new{@class="easyui-validatebox"})
                    @Html.DisplayDescriptionFor(model => model.Name)</td> 
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.Title)</th>
                <td>@Html.EasyuiInput(model=>model.Title,new{@class="easyui-validatebox"})
                    @Html.DisplayDescriptionFor(model => model.Title)</td>
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.Url)</th>
                <td>@Html.EasyuiInput(model => model.Url, new { @class = "easyui-validatebox" })
                    @Html.DisplayDescriptionFor(model => model.Url)</td>
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.LogoUrl)</th>
                <td>@Html.EasyuiInput(model => model.LogoUrl, new { @class = "easyui-validatebox" })
                    @Html.DisplayDescriptionFor(model => model.LogoUrl)</td>
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.MetaDescription)</th>
                <td>
                    @Html.EasyuiInput(model => model.MetaDescription, new { @class = "easyui-validatebox" }, "textarea")
                    @Html.DisplayDescriptionFor(model => model.MetaDescription)</td>
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.MetaKeywords)</th>
                <td>@Html.EasyuiInput(model => model.MetaKeywords, new { @class = "easyui-validatebox" }, "textarea")
                    @Html.DisplayDescriptionFor(model => model.MetaKeywords)</td>
            </tr>
            <tr>
                <th>@Html.LabelFor(model => model.Copyright)</th>
                <td>@Html.EasyuiInput(model => model.Copyright, new { @class = "easyui-validatebox" }, "textarea")
                    @Html.DisplayDescriptionFor(model => model.Copyright)</td>
            </tr>
            <tr>
                <th></th>
                <td><a id="save" href="javascript:void()" class="easyui-linkbutton">保存</a></td>
            </tr>
        </table>
    </div>
}
<script type="text/javascript">

    $(document).ready(function () {
        $("#save").click(function () {
            ConfigSubmit();
        });
    });
</script>

在\Areas\Admin\Scripts文件件中添加System.js文件

//Config页提交
function ConfigSubmit() {
    $('#siteconfig_form').form('submit', {
        success: function (data) {
            var rt = jQuery.parseJSON(data);
            if (rt.Success) {
                $.messager.alert("保存成功", rt.Message);
            }
            else {
                var msg = "";
                if (rt.MessageLsit != undefined) {
                    $.each(rt.MessageLsit, function (i, val) {
                        msg += "<li>" + i + ":" + val + "</li>";
                    });
                }
                if (msg != "") msg = rt.Message + "<br /> <p> 原因如下:" + "<ul>" + msg + "</ul></p>";
                else msg = rt.Message;
                $.messager.alert("保存失败", msg, "error");
            }
        }
    });
}

完工

image

前台的调用:

打开前台布局页_Layout.cshtml。

在顶部获取网站信息

image

随后在布局页调用相应字段

image

进首页看一下,已经显示出来了。

image

==========================

代码见http://pan.baidu.com/s/1rG1vH

发表评论
用户名: 匿名