0

i have this code as i need to load a url.action with different parameters in a loop (multiple links)

Im having a couple of problems, first only the first link in the foreach loop actually does anything

Second problem is the controller is expecting id as an int and name as a string which isnt working so nothing loads? if i change the id and string in the 'link' manually it works

I have put both the values into a window.alert and it shows the javascript is getting the correct values, just dont think they are being recieved by the controller as correct string/int??

Javascript

<script type="text/javascript">

$('#changetopicss').click(function () {
    $('#loadingAjaxs').show();
    $('#flubestext').hide();

    var link = '@Url.Action("Detail", "Topics", new { @id = -1,  @name = -2, @forum = "all", @page = 0})';


          link = link.replace("-1", $(this).data('id'));
          link = link.replace("-2", $(this).data('shortname'));
 //   window.alert(dataname);
        $('#contentwrap').load(link, function () {
            $('#loadingAjaxs').hide();
            $('#flubestext').show();
            window.history.pushState(null, 'title', '/topics');
        });
    });
</script>

Links created dynamically in a for each loop

<a id="changetopicss" data-id='@following.Id' data-shortname='@following.ShortName'>@following.Title</a>

Controller Header

public ActionResult Detail(int id, string name, string forum, int page)
4

1 回答 1

1

1) changetopicss should be a 'class' not 'id' of the anchor. As id is expected to be unique across the page, only the first element with this id is selected and attached the click handler.

2) its safe to URLEncode strings before using them in URLs like in your code shortname could have space in it, use javascript's encodeURIComponent()

于 2013-09-29T18:19:22.007 回答