asp .net mvc实现 富文本框

首先 我使用的是summernote 具体库参考  Summernote 中文网 | Summernote 是一个简单灵活的所见即所得的 HTML 在线编辑器,基于 jQuery 和 Bootstrap 构建,支持快捷键操作,提供大量可定制的选项。 该网站

先放代码

<!DOCTYPE html>
<htmllang="en">
<head>
  <metacharset="UTF-8">
  <title>Summernote</title>
  <linkhref="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css"rel="stylesheet">
  <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
  <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 
  <linkhref="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.css"rel="stylesheet">
  <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.12/summernote.js"></script>
</head>
<body>
<form method="post" id="NotForm" action="/TNotices/addByNottice">
<p>标题:<input type="text" style="width:95%; height:30px; border :solid 1px #ccc; border-radius:3px; margin-top:5px;" name="Title" id="Title" /></p>

<textarea name="Content" class="summernote" id="contents" title="Contents"></textarea>
@*富文本框*@
<input type="text" style="display:none;" name="ContentText" value="" id="ContentText" />
@*把富文本框的值给他 但是隐藏了*@
<p><input type="submit" style="background-color: #97c8ff; color:white; border:0px; border-radius:3px; float:right;" value="发布" onclick="addbtn()" /></p>
</form>

  <script>
///初始化富文本框属性样式
$('.summernote').summernote({
height: '380px',
lang: 'zh-CN',
toolbar: [
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'clear']],
['fontname', ['fontname']],
['fontsize', ['fontsize']],
['color', ['color']],
['para', ['ol', 'ul', 'paragraph', 'height']],
['table', ['table']],
['insert', ['link']],
['view', ['undo', 'redo', 'fullscreen', 'codeview', 'help']]
]

});

</script>
</body>
</html>

代码如上  前端图片

打开的样子就是这样  我没有上传图片和视频 于是把他初始化了

其次:如果要将文本框内容上传到数据库的话  ,需要借助一个文本框了,上面的代码已经加入了

如何我们需要做的操作是 获取富文本框的赋值给借助的文本框(id=ContentText)

然后再获取这个文本框的值上传到数据库

这样就已经取到值了 接下来是控制器代码  在控制器里面创建一个addByNottice 方法 代码如下:

[HttpPost]
        [ValidateInput(false)]

        public ActionResult addByNottice(string Title,string ContentText)
        {
            
           var date = DateTime.Now.ToString("yyyy-MM-dd");

            Notice notice = new Notice()
            {
                Title = Title,
                Content = ContentText,
                Time=DateTime.Parse( date),
                TeacherID = int.Parse(Session["TeacherID"].ToString())
                
            };
            db.Notice.Add(notice);
            db.SaveChanges();
            return RedirectToAction("Index");//重定向
        }

另外附上 数据库存的 样子

 是带有标签的  所以 视图使用时 必须转换 @htmlRaw  如 <h4 class="nei">@Html.Raw(notice.Content)</h4>  原本显示内容是@notice.Content

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫.net

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值