0

In the following ActionLink id like to set PartyId & IsFollowing with two jQuery vars

@Ajax.ImageActionLink("ToggleEnabled", "Following Status",
                                    new { id = PartyId },
                                    new AjaxOptions
                                    {
                                        UpdateTargetId = "ti" + PartyId,
                                        InsertionMode = InsertionMode.Replace,
                                    },
                                    IsFollowing,
                                    "../../Images/tick.png",
                                    "../../Images/tick_grey.png",
                                    PartyId)

How can I do this?

2
  • You can't do that because the HTML helper runs on the server, much before any javascript variables to exist. You will have to do this on the client using javascript. What is the ImageActionLink helper doing? That's not a standard helper. Commented Mar 9, 2012 at 13:26
  • I modified it to just toggle an image depending on a bool - so its a toggle image action link, but yes I agree I now understand it cant be done. I am sat right now re-writing this to use jQuery with $ajax instead. Thanks Darin Commented Mar 9, 2012 at 14:08

1 Answer 1

1

My Solution

As this cant be done the way I wanted, here is a jQuery version for completeness : This will toggle an image if clicked, update the DB via $.Ajax & change image to toggled version

HTML

<a id="HD_FollowingLink">
  <img id="HD_FollowingImage" />
</a>

jQuery & JavaScript

$('#HD_FollowingLink').click(function () {
        // 'Following' Toggle Button has been clicked, get its 'id' & use ajax to toggle DB, display ajax returned data.IsFollowing (which should be toggled)
        var id = $(this).attr('id');              
        $.ajax({
            url: "/Home/ToggleEnabled/" + id,
            type: "POST",
            data: ({ id: id }),
            success: function (data, textStatus) {
                ToggleFollow(data.IsFollowing, id);
            }
        }); //$.ajax end
    });

function ToggleFollow(IsFollowing, id) {
// Called by LoadHoverDivData & HD_FollowingLink $.Ajax calls
$('#HD_FollowingLink').attr("id", id); //Set this id so we can use it in     
$('#HD_FollowingLink').click handler


if (IsFollowing == true) {
$('#HD_FollowingImage').attr("src", "../../Images/tick.png");

} else {
$('#HD_FollowingImage').attr("src", "../../Images/tick_grey.png");
}
};

MVC Action

public ActionResult ToggleEnabled(int id)
{
if (Request.IsAjaxRequest())
{
// Get if party being followed 
int PID = ViewBag.ThisParty.PartyId;
var f = db.Followings.SingleOrDefault(q => q.PartyFollowedID== id && q.ThisPartyId == PID);
f.following = f.following != true;// Toggle bit
db.Entry(f).State = System.Data.EntityState.Modified;
db.SaveChanges();// Save toggled bit back to DB

return Json(new {IsFollowing = f.following });
}
return Content("Error");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.