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.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
user3853986
  • 15
  • 1
  • 1
  • 8
  • 2
    Do you understand the difference between code that runs on the server and code that runs on the client? – Erik Philips Jul 18 '14 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. – Mark S Jul 18 '14 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. – user3853986 Jul 18 '14 at 18:06
  • You can use the technique from this [answer](http://stackoverflow.com/questions/18311503/how-to-pass-a-razor-value-to-a-jquery-function-in-an-mvc-view?rq=1) – Jasen Jul 18 '14 at 18:08

1 Answers1

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.

charles
  • 547
  • 1
  • 3
  • 11
  • Sesseion["UserId"] will just give me the UserId of last row added to the table regardless of which row I click on. – user3853986 Jul 18 '14 at 18:09
  • Why are you storing this in the session? That should be a javascript variable. Regardless, I'd advise passing the id through the element. Instead, just use a data-id attribute on your tr _if its truly necessary_. – charles Jul 18 '14 at 18:13