데이터베이스에서 검색 한 여러 기사에 대해 위/아래 투표 시스템을 구축하고 싶지만 각 기사에 쿠키를 추가하여 투표 수가 제한되므로 하루 만에 만료됩니다. 적절한 코드를 어디에 추가해야할지 모르겠다.위/아래 투표 시스템에서 쿠키 사용
자세한 내용 :
<script>
function vote(id, value) { // function vote with 2 arguments: article ID and value (+1 or -1) depending if you clicked on the arrow up or down
var dataFields = { 'id': id, 'value': value }; // We pass the 2 arguments
$.ajax({ // Ajax
type: "POST",
dataType: "text",//This for indicate that you'r expecting a text response from server
url: "WebService.asmx/updateVotes",
data: dataFields,
timeout: 3000,
success: function (dataBack) {
if(
$('#number' + id).html(dataBack);// div "number" with the new number
$('#arrow_up' + id).html('<div class="arrow_up_voted"></div>'); // We replace the clickable "arrow up" by the not clickable one
$('#arrow_down' + id).html('<div class="arrow_down_voted"></div>'); // We replace the clickable "arrow down" by the not clickable one
$('#message' + id).html('<div id="alertFadeOut' + id + '" style="color: green">Thank you for voting</div>'); // Diplay message with a fadeout
$('#alertFadeOut' + id).fadeOut(1100, function() {
$('#alertFadeOut' + id).text('');
});
},
error: function() {
$('#number' + id).text('Problem!');
}
});
}
</script>
위의 코드는 위쪽 화살표 하나마다 사용자가 클릭 당 투표 수를 증가하고 반대로 감소하는 스크립트 호출 아약스 방법이다.
public string updateVotes(string id,int value)
{
System.Threading.Thread.Sleep(500); // delay for 2.5 seconds Network latency
post p = db.posts.Find(int.Parse(id));
// assign new values
p.totalVotes += value;
db.Entry(p).State = System.Data.EntityState.Modified;
db.SaveChanges();
string dataBack =p.totalVotes.ToString();
return dataBack;
}
이것은 웹 메소드입니다.
이제 큰 소리로 생각해 보았습니다. 쿠키가 null인지 아닌지에 관계없이 다음 함수를 ewxamine에 코딩합니다.
public bool enableVoting()
{
HttpCookie cookie = Request.Cookies["enableVote"];
if (Request.Cookies["enableVote"] != null)
{
return true;
}
else
{
return false;
}
}
나는 잘못 알고 있지만 적어도 시도했습니다.
또한 어디에 사용자가 기사에 투표 할 때마다 쿠키를 추가 할 각 루프를 추가 할 것인가?
foreach(post p in db.posts){
HttpCookie cookie = new HttpCookie("enableVote"+p.ID);
cookie.Value = "article:"+p.ID;
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
조언 해 주셔서 감사합니다. 유권자의 신분을 얻는 방법을 설명하기위한 예제 또는 링크가 있습니까? –
Asp.net C#을 사용하면 그렇게 할 수 있습니까? –