在cshtml中增加操作

时间:2014-07-09 06:37:12

标签: sql razor asp.net-webpages

我想在点击按钮时增加属性的值,即当用户点击按钮时,属性(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

任何帮助?

1 个答案:

答案 0 :(得分: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"]完成相同的结果:返回&#34;喜欢&#34;价值(所以我不能真正理解你的代码的含义)。