【问题标题】:Increment operation in cshtmlcshtml中的增量操作
【发布时间】:2014-07-09 15:22:42
【问题描述】:

我想在单击按钮时增加属性的值,即当用户单击按钮时,属性 (int) 的值将增加 1。

这是我增加值的代码:

if(IsPost){
    if(Request["like"] == forum_topic_id){
        //get the value of the forum post id from the button
        forumpostid = Request.Form["like"]; 
        var likepostcommand = "UPDATE forum_post SET forum_post_up = forum_post_up + 1 WHERE forum_post_id = @0";
        var likepostdata = db.Query(likepostcommand, forumpostid);
    }
}

触发这个操作的button是:

 <button type="submit" class="btn btn-default" name="like" value="@getforumtopicdetailsdata.forum_thread_id">Like</button>

按钮在表单内。

单击按钮时,属性forum_post_up 的值应增加1 值。

有什么帮助吗?

【问题讨论】:

  • 你可以使用ajax调用来完成这个任务
  • 你能解释一下吗? @HirenKagrana

标签: sql razor asp.net-webpages


【解决方案1】:

你的代码应该是

if(IsPost){
    if(Convert.ToInt32(Request["like"]) == forum_topic_id){
        //get the value of the forum post id from the button
        forumpostid = Convert.ToInt32(Request.Form["like"]); 
        var likepostcommand = @"UPDATE forum_post SET forum_post_up = forum_post_up + 1 
            WHERE forum_post_id = @0";
        var likepostdata = db.Execute(likepostcommand, forumpostid);
    }
}

注意Request["like"]Request.Form["like"] 完成相同的结果:返回“like”值(所以我不太了解您的代码的含义)。

【讨论】:

  • 代码不会增加数据库中的任何值。我不知道怎么做!单击按钮时,forum_post_up 的值应增加一个值1。 cshtm 中的查询 set forum_post_up = forum_post_up + 1 是否正确?
  • 你确定Convert.ToInt32(Request["like"]) == forum_topic_id 是真的吗?在forumpostid = Convert.ToInt32(Request.Form["like"]); 之后添加&lt;p&gt;@forumpostid&lt;/p&gt;。如果 if 条件为真,那么在页面的开头您应该会看到 forumpostid 的值。验证您的表中是否存在与其值匹配的有效 forum_post_id。
  • 你的答案是正确的。实际上我在其他地方做错了! @gmg
猜你喜欢
  • 2021-06-29
  • 2015-10-15
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多