1
我想让微博等其他用户可以关注我的帖子。更改按钮状态(MVC)
因为我制作了所有当前注册用户的页面。在每个名字前面都有按钮可以关注/取消关注用户。 (如在微博)
视图 -
@{
ViewBag.Title = "Users";
}
@model MembershipUserCollection
@foreach (MembershipUser item in Model)
{
if(User.Identity.Name != item.UserName)
{
<li>@item.UserName
<span id="[email protected]"><input id="@item.UserName" name="submit" type="submit" value="Follow" class="follow-user fg-button ui-state-default"/></span>
</li>
}
}
<script type="text/javascript">
$(".follow-user").live("click", function (e) {
e.preventDefault();
var data = $(this).attr("id");
var spid = '#sp-' + data;
var btnid = '#' + data;
var val = $(this).attr('value');
$.ajax({
type: "POST",
url: "User/FollowUser",
data: { id: data },
cache: false,
dataType: "json",
success: function() {
if (val == 'Follow') {
$(btnid).attr('value', 'Unfollow');
}
else {
$(btnid).attr('value', 'Follow');
}
}
});
});
</script>
控制器 -
public ActionResult Index()
{
return View(Membership.GetAllUsers());
}
public void FollowUser(string id)
{
ViewData["test"] = "test";
var n = FollowingUser.CreateFollowingUser(0);
n.FollowingId = id;
n.FollowerId = User.Identity.Name;
string message = string.Empty;
var list = new List<FollowingUser>();
list = (from a in db.FollowingUsers where a.FollowerId == User.Identity.Name && a.FollowingId == id select a).ToList();
if (list.Count() == 0)
{
try
{
db.AddToFollowingUsers(n);
db.SaveChanges();
}
catch (Exception ex)
{
message = ex.Message;
}
}
else
{
db.DeleteObject((from a in db.FollowingUsers where a.FollowerId == User.Identity.Name select a).FirstOrDefault());
db.SaveChanges();
}
}
关注表 -
现在我想在页面加载检查数据库时更改按钮状态,不管他是否已被遵循。例如:
例如,如果用户已经遵循它应该显示如下。
我还必须制作模型吗? – ChamingaD
检查,我更新了我的答案。 – Parminder
谢谢。你能解释这条线吗? SomeRepository.IsFollowing(LoggedInUser.Id,id) 什么是SomeRepository,IsFollowing,LoggedInUser.Id和id? – ChamingaD