我正在使用ASP.NET MVC,我試圖建立一個投票系統,類似於stackoverflow。在ASP.NET MVC中類似於SO的投票系統
我希望當我點擊投票按鈕,在一個動作上發表帖子,在那裏做一些檢查,但留在我的初始頁面上,並增加投票(用js),如果檢查通過像SO)。
我要投票通過索引操作被填充的項目
查看
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div><input type="submit" name="Vote" value="" class="fa fa-angle-up"/>
</div>
<div>@Html.DisplayFor(modelItem => item.Votes)</div>
<div><input type="submit" name="Vote" value="" class="fa fa-angle-down" /></div>
}
行動
public ActionResult SendVote(string vote)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap<VoteLogViewModel, VoteLog>());
var mapper = config.CreateMapper();
switch (vote)
{
case "":
if (ModelState.IsValid)
{
//Send to db
VoteLogViewModel voteLogViewModel = new VoteLogViewModel
{
DateAdded = DateTime.Now,
Id = Guid.NewGuid().ToString(),
PlaceId = id,
UserId = User.Identity.GetUserId(),
Vote = 1
};
db.VoteLogs.Add(mapper.Map<VoteLog>(voteLogViewModel));
db.SaveChanges();
}
else
{
return RedirectToAction("Index");
}
break;
case "":
if (ModelState.IsValid)
{
//Send to db
}
else
{
return RedirectToAction("Index");
}
break;
}
return new EmptyResult();
}
如何投票,而無需重新加載整個頁面?
我應該只是在我的投票圖標下做一些鏈接,並以某種方式處理這個路由?
如果您想保留在同一頁面上,您需要使用ajax發佈該值。 –
您可以編寫jQuery從控制器調用ActionMethod。 –