首页 新闻 会员 周边

MVC网页中实现类似微博的评论功能

0
[已解决问题] 解决于 2014-03-06 16:24

如上图,是一个textarea文本框和一个按钮,想要的效果是:在文本框中输入评论内容,点击“发表”按钮,可以获取文本框中输入的内容,并保存到Mysql数据库,我的做法是用js获取输入的内容

var comment_txt = document.getElementById('comment').value

然后连接数据库

var con = new ActiveXObject("ADODB.Connection");
con.ConnectionString = "SERVER=10.6.1.190;User ID=root;Password=hyron-123;Database=blog;Port=3306";

最后执行插入语句

con.open;
var rs = new ActiveXObject("ADODB.Recordset");
rs.open("INSERT INTO `blog`.`blog_comment` (`comment_id`, `reply_id`, `blog_id`, `comment_auth`, `comment_time`, `comment_enable`, `comment_txt`, `delete_time`) VALUES (8, 0, 1, 'w', '2014-02-27 17:42:39', 1, @comment_txt, GETDATE())", con)

但是没有成功,求大神帮忙看看,哪里的问题?或者教个其它方式,谢谢!

Deeper Love的主页 Deeper Love | 初学一级 | 园豆:171
提问于:2014-02-28 18:18
< >
分享
最佳答案
0

断点调试

奖励园豆:5
单恋 | 小虾三级 |园豆:678 | 2014-03-04 17:28
其他回答(2)
0

@using (Html.BeginForm("publish_comment", "comment"))
{
    <div>
        <input type="hidden" name="blog_id" value="@ViewBag.blog_id" />
        <textarea id="comment" name="comment_txt" style="width:590px;height:100px;" resize="none" maxlength="500" onkeypress="return imposeMaxLength(this)" onblur="ismaxlength(this)" placeholder="说点什么吧...(字数在500字以内)">@ViewBag.comment_txt</textarea>
    </div>

    <div style="width:50px;height:20px;margin-left:530px;margin-top:10px;">
        <input name="publish" type="submit" value="发表" />
    </div>
}

通过submit向页面提交事件,将参数传到controller,再传给service方法,在service中向数据库中插入数据,代码:

public static void PublishComment(Models.CommentResult model)
        {
            if (model.comment_txt == null)
            {
                
            }
            using (Models.blogEntities edm = new Models.blogEntities())
            {
                int rows = edm.blog_comment.Count() + 1;

                edm.blog_comment.AddObject(new Models.blog_comment()
                {
                    comment_id = rows,
                    blog_id = model.blog_id,
                    comment_txt = model.comment_txt,
                    comment_time = System.DateTime.Now,
                    comment_enable = true,
                    comment_auth = model.comment_auth
                });
                edm.SaveChanges();

            }
        }

也不知道说的对不对,但终究是解决了,请多指正!

Deeper Love | 园豆:171 (初学一级) | 2014-03-06 16:23
0

3333333

cherryzhang | 园豆:202 (菜鸟二级) | 2016-12-19 16:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册