开发者

MVC - Rendering Action Links When Working With JSON

I'm working on 开发者_如何学Goan MVC application where I have a tabbed navigation where the contents update using the JSON results from an AJAX call (I'm using JQuery).

I will be rendering a list of X items and each item will have there action links associated with them which will in turn post data back to the server via JSON.

If we were not using JSON to populate the page then I would just use Html.ActionLinks. However I'm thinking I will need to build up the links in Jquery. Two questions

  1. Is this sensible or is there a better way to do this
  2. Is injecting links into pages via JQuery secure?


Returning data from server in JSON doesn't mean that the request data also should be in JSON, right? As you use it for navigation, I suppose you should GET the data from server. Why won't you just use the ordinal links for getting data? Generating action links is much more simpler, server will understand values from query string and the returned json can be handled as you wish. Just generate those ordinary links with Html.ActionLink that include route values for navigation specific data. And the small jQuery could turn them into asynchronous ones

<script type="text/javascript">
        $(function () {
            ('a .content-navigation-link').click(function () {
                var $this = $(this);
                $.getJSON($this.attr('href'), function (data) {
                    //do whatever you wish with the returned json data
                });
            });
        });
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜