0

I have a HTML table with multiple columns and a switch. I show the information from a database in table. The table has a switch. When user clicks on the switch I want to pass the UserId to AJAX function and run a script, but I'm not able to retrieve the value in javascript function.

Here is part of the HTML Razor code in question:

@model IEnumerable<WebApplication8.Models.ManageUserViewModel>
@foreach (var item in Model)
{

    string UserId = @Html.DisplayFor(model => item.UserId).ToString();
    Session["UserId"] = UserId;    
    <tr>
        <td style="display:none" id="UserId">@UserId</td>
        <td align="center">
            <div class="btn-group btn-toggle">
                @if (item.ActiveUser)
                {
                    <button class="btn btn-sm btn-success active" >ON</button>  
                    <button class="btn btn-sm btn" onclick="test()">OFF</button>  
                }
                else
                {
                    <button class="btn btn-sm btn" onclick="On()">ON</button>
                    <button class="btn btn-sm btn-success active">OFF</button>
                }
            </div>



        </td>
   </tr>
 }

Here is mini-test function where I'm trying to get the value of UserId:

function test(){

     var UserId = document.getElementById('UserId');
     alert ('UserId');
    }

Any suggestion/workaround would be hugely appreciated.

4
  • 2
    Do you understand the difference between code that runs on the server and code that runs on the client? Commented Jul 18, 2014 at 17:59
  • 1
    If you have more than one row, you're also going to have id collisions. They'll all be named UserId. Commented Jul 18, 2014 at 18:05
  • @ErikPhilips, I understand Javascript code is a client code; however, was wondering if it is possible to pass data between server-client code. Or any other way that I can identify user's input. Commented Jul 18, 2014 at 18:06
  • You can use the technique from this answer Commented Jul 18, 2014 at 18:08

1 Answer 1

0

You're using ajax, so this isn't necessary. Just use Session["UserId"] in whatever action your ajax is pointing to.

Edit: Besides, what you're trying to do here is easily manipulated by the client to where they'll be able to access other user accounts in this fashion. You'll need some server side code to authenticate the user who he says he is anyhow... just leave all the user info on the server.

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

2 Comments

Sesseion["UserId"] will just give me the UserId of last row added to the table regardless of which row I click on.
Why are you storing this in the session? That should be a javascript variable. Regardless, I'd advise passing the id through the <td> element. Instead, just use a data-id attribute on your tr if its truly necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.