开发者

Use Json and AjaxLink to Toggle Link Values in ASP.NET MVC 2

I have a list of Child objects. I would like to be able to toggle whether they are included in a smaller list of objects which have a relationship to a Parent object. Like so:

<% foreach(var child in Model.AllPossibleChildren)%>
<% { %>
<tr>
    <td>
        <%if(child.Link.Any(r => r.Id == Model.Parent.Id))
            { %>
            <%:Ajax.ActionLink("Remove", "RemoveChild", new { aId = Model.Parent.Id, rId = child.Id }, null)%>
        <% } 
            else 
        { %>
            <%:Ajax.ActionLink("Add", "AddChild", new { aId = Model.Parent.Id, rId = child.Id }, null)%>
        <% } %>
    </td>
</tr>
<% } %>

The idea here is that when the user clicks on "Remove", it will remove the relationship between the Parent and Child, and then somehow (using Jquery or AjaxOptions) toggle the ActionLink parameters to match the other condition (Add instead of Remove for link text, AddChild instead of RemoveChild for action method). That's the part I'm unclear on.

  1. Am I approaching this correctly, or is there a better way to do this?
  2. If this is the right approach, which would be better to use - AjaxOptions or JQuery (I'm guessing the answer is JQuery, since it's more flexible)
  3. Depending on the answer to #2, how would I do that?

Incidentally, here's my action method. I tested that they are called correctly and pass the correct parameters.

    public JsonResult AddChild(string aId, int rId)
    {
        //Create Relationship
        return Json(result);
    }

    public JsonResult RemoveChild(string aID, int rID)
    {
        //Remove Relationship
        return Json(result开发者_如何学JAVA);
    }

Thanks!

UPDATE I've investigating using partialviews for this, but can't seem to get that to work either. I keep coming back to this model, and continue to not be able to figure out how to get it to work! I don't want to have to repost the whole page, although with the way I have it set up right now it would work. Perhaps that's my problem? Still looking for some help!


I gave up on using Ajax.ActionLink and went with a straight div/ jquery ajax approach. Not that I knew how to do that either...see my subsequent question and my final working answer here: Can't get jquery $ajax to call .NET MVC 2 controller method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜