0

Eg.

 $(".button").click(function () {
                var index=$(this).closest('tr').index();
                var [email protected][index];               
            });

In this,index is javascript and @Model.Task is server side so error say "The name index does not exist in the current context".

4
  • because index is javascript variable and you are using it with C# variable. Commented Oct 13, 2016 at 7:15
  • 1
    What you are trying to do is impossible. You need to understand the difference between server-side and client-side Commented Oct 13, 2016 at 7:16
  • You can use @html.Hidden field for the same. set hidden field with javascript variable and get it while accessing with C# variable. Commented Oct 13, 2016 at 7:20
  • 2
    You can initialize a JS array using @Model value, and process it as a JS array. Commented Oct 13, 2016 at 7:21

2 Answers 2

2

Before on click function, you should do below.

var tasks = JSON.parse('@Html.Raw(Model.Task)');

After then you can do

$(".button").click(function () {
            var index=$(this).closest('tr').index();
            var records = tasks[index];               
        });
Sign up to request clarification or add additional context in comments.

2 Comments

you're welcome, but as other people said, this a temporary solution for you, only for this problem. My suggestion is, you should know differences between client side and server side before everything.
yep i am beginner of javascript and sometime i was confused with which is client code and which is server code.thanks for suggestion.
0

as it was told by vivek and Andrei - it is impossible. index is variable from javascript, and Model.Task is array from model. they are executed in different moment. think about it that way. All values obtained from Model are executed/parsed by server before sending response to client (browser). it means that client sees them as normal text, not as an object. But javascript is executed on client (browser) AFTER page was downloaded and rendered (and the code you provided is executed even later - after button was clicked). This means that when Model is parsed it has "no idea" about javascript.

the only way to do what you want to do is what KnowGe posted - save all Model.Task table as javascript object and then use this object in your listener

Comments

Your Answer

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