0

i am using asp.net mvc 3. in one my of page. i need to get a list of string from a list object. so i do this:

@{
    var orderIds  = from s in Model.Orders
                    select s.id;

}

and from one of my ajax call, i will need "orderIds "

$("#renderBtn").click(function () {
            var inputData = {
                'orderIds':  need to get the order ids here 
            }; 

            $.ajax({
                url: '/Order/ExtraData',
                data: inputData,
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                    ....
                },
                error: function () {
                    ...
                }
            });
        });

any idea how can i get orderIds and use in javascripts? Thanks

2
  • the controller needs to give this information to the view (which is the jquery code). so create a route /Order/ExtraData for this controller. (i don't know asp.net, so i can't give you more details) Commented Jun 17, 2012 at 1:30
  • i construct a hidden value, the view populate value and store in hidden filed.. then javascript read from it.. solve my problem :) Commented Jun 17, 2012 at 1:34

2 Answers 2

2

Your code aboce defining orderIds is a server-side variable; what you need us a client-side variable. You'll need to define that variable at the creation of the view, in a script block near the bottom of the page.

@{ 
  var orderIds = string.Join(",", Model.Orders.Select(s => s.id).ToArray());
}
<script>
       var orderIds  = [@orderIds]
</script>

Caveat: The code may not be perfect, and may not compile, but that should give you an idea how to proceed.

Sign up to request clarification or add additional context in comments.

Comments

1

If you aren't (or don't want to) using inline javascript and keep it all separated you could do something like this:

@{ 
  var orderIds = string.Join(",", Model.Orders.Select(s => s.id).ToArray());
}

<input type="hidden" id="orderIds" value="@orderIds" />

And then in your javascript/jQuery:

orderIds = $('#orderIds').val();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.