简体   繁体   中英

MVC - Rendering Action Links When Working With JSON

I'm working on an 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM