I'm working on an app that has up and down voting similar to StackExchange.
I want to be able to have the following
- user clicks up vote, and a record in the vote table gets populated with 1
- user clicks up vote again and the record is deleted
^^ same thing happens with down votes
or
- user clicks up vote, and a record in the vote table gets populated with 1
- user clicks down vote instead, and the original record is deleted, and a new record is added with -1
My table looks like this
USE [MyProject]
GO
/****** Object: Table [dbo].[EventVotes] Script Date: 02/21/2011 15:16:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[EventVotes](
[UserID] [int] NOT NULL,
[EventID] [int] NOT NULL,
[Vote] [int] NOT NULL,
[VoteDate] [datetime] NOT NULL,
CONSTRAINT [PK_EventVotes_1] PRIMARY KEY CLUSTERED
(
[UserID] ASC,
[EventID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[EventVotes] WITH CHECK ADD CONSTRAINT [FK_EventVotes_Events] FOREIGN KEY([EventID])
REFERENCES [dbo].[Events] ([ID])
GO
ALTER TABLE [dbo].[EventVotes] CHECK CONSTRAINT [FK_EventVotes_Events]
GO
ALTER TABLE [dbo].[EventVotes] WITH CHECK ADD CONSTRAINT [FK_EventVotes_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([ID])
GO
ALTER TABLE [dbo].[EventVotes] CHECK CONSTRAINT [FK_EventVotes_Users]
GO
I need a good concept in how to implement this. Do I call a VoteController
Action and send it two params? bool isUpDown
and int Vote
If isUpDown Then
MyRepository.Delete(EventID, UserID)
MyRepository.Sub开发者_运维技巧mitChanges()
End If
MyRepository.Insert(EventID, UserID, Vote)
MyRepository.SubmitChanges()
I don't know if there are any holes in this logic, or if there is a better way to achieve this. The plan is to do it all via AJAX.
Also, should the view have two forms (one for UP and one for DOWN), or should I try an manage more with jQuery? I'm just not sure how to approach this particular problem.
Try to implement it with jQuery and one Action like with two arguments bool upDown and int EventID. Your action can return a JSON with the result (accept or reject depending on if the user has voted previously) and the current votes for that event.
精彩评论