asp.net mvc - Ajax.ActionLink repeating the same exact GET request multiple times -
i'm learning mvc , making simple scrum tracking system go along.
the problem i'm having when ajax.actionlink clicked, run same ajax action once every scrum card displayed on page.
as can see, have 9 cards displayed , 9 identical requests. (the action link color wheel image in lower right hand side of card).
singlecard.cshtml (view) - "colorpicker" name of action.
<script src="@url.content("~/scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> ... <div class="card_footer" id="card_footer_id_@(model.id)"> <div class="card_tags"> [tag1] [tag2] [tag3] </div> <div class="card_colorwheel_icon"> @ajax.imageactionlink("../content/images/color_wheel.png", "color wheel", "colorpicker", new { cardid = model.id }, new ajaxoptions { updatetargetid = "colorpickerdisplay" }) </div> </div>
the imageactionlink helper i'm using, works normal actionlink
homecontroller.cs (controller)
public actionresult colorpicker(int cardid) { var currentcard = db.cards.single(x => x.id == cardid); var colors = new list<cardrgb>(); var cards = db.cards.tolist(); foreach (var card in cards) { colors.add(new cardrgb { cardid = card.id, red = (int)card.bg_red, blue = (int)card.bg_blue, green = (int)card.bg_green }); } // disctint var model = new colorpickerviewmodel() { colors = colors, red = (int) currentcard.bg_red, green = (int) currentcard.bg_green, blue = (int) currentcard.bg_blue }; return partialview(model); }
does know why code running once per card?
edit: requested!
public static class imageactionlinkhelper { public static mvchtmlstring imageactionlink( ajaxhelper helper, string imageurl, string alttext, string actionname, object routevalues, ajaxoptions ajaxoptions) { var builder = new tagbuilder("img"); builder.mergeattribute("src", imageurl); builder.mergeattribute("alt", alttext); builder.mergeattribute("title", alttext); var link = helper.actionlink("[replaceme]", actionname, routevalues, ajaxoptions); var html = link.tohtmlstring().replace("[replaceme]", builder.tostring(tagrendermode.selfclosing)); return new mvchtmlstring(html); } }
check html of page. in singlecard.cshtml there line:
<script src="@url.content("~/scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
for every card again include javascript, included 9 times. therefore 9 requests sent server.
solution: put script-include on page level, not on card level.
Comments
Post a Comment