更改按钮状态(MVC)

时间:2011-12-22 04:42:27

标签: c# asp.net-mvc asp.net-mvc-3

我想像微博网站一样制作Twitter,其他用户可以关注我的帖子。

对于我所有当前注册用户的页面。在每个名称前面都有跟随/取消关注用户的按钮。 (比如Twitter)

Follow User Page

查看 -

@{
    ViewBag.Title = "Users";
}
@model MembershipUserCollection

@foreach (MembershipUser item in Model)
{
if(User.Identity.Name != item.UserName)
{

     <li>@item.UserName 
        <span id="sp-@item.UserName"><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();
    }
}

FollowUsers表 -

FollowingUsers Table

现在我想在页面加载检查数据库上更改按钮状态,无论他是否已被跟踪。

Ex-如果用户已经关注它,则应显示如下。

Unfollow

1 个答案:

答案 0 :(得分:2)

当您向显示此按钮的用户显示此视图时,如果此人正在关注,也请加载状态。

public ActionResult Index()
{
    var model  = new MemberShipViewModel();
    //We check here if the logged in user is already following the user being viewd

    foreach(var member in  Membership.GetAllUsers())
    {

      var user = (from a in db.FollowingUsers where a.FollowerId == User.Identity.Name && a.FollowingId == member.UserName select a).FirstOrDefault();
     model.Members.Add(new Member{UserName = member.UserName,IsFollowing=user!=null});
    }
    //This line will remove the logged in user.   
    model.Members.Remove(model.Members.First(m=>m.UserName==User.Identity.Name)); 


    return view(model);


}

在索引视图模型中,您需要进行一些更改。

 @model MemberShipViewModel

 @foreach (var item in Model)
 {

 <li>@item.UserName 

if(!item.IsFollowing)
{

    <span id="sp-@item.UserName"><input id="@item.UserName" name="submit" type="submit" value="Follow" class="follow-user fg-button ui-state-default"/></span> 
 }
 else
 {

    <span id="sp-@item.UserName"><input id="@item.UserName" name="submit" type="submit" value="Follow" class="unfollow-user fg-button ui-state-default"/></span> 

}


 </li>

}

    $(“。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');         $就({             类型:“POST”,             url:“User / FollowUser”,             数据:{id:data},             cache:false,             dataType:“json”,             成功:function(){                 if(val =='Follow'){                     $(btnid).attr('value','取消关注');                 }                 其他{                     $(btnid).attr('value','Follow');                 }             }         });     });

你现在需要写一些javascript。没有人会为你编写完整的软件。 看起来你缺少非常基本的编程技巧。

欢呼声