Kay
Kay

Reputation: 350

How to pass object into function in JavaScript

I am doing my assignment about ASP.NET MVC and ASP.NET WEB API I have database like:

ID, FirstName, Middle, LastName, DoB, Gender.

And I am using EF and ADO.NET to make connection from database to ASP.NET. I write a function DoubleClickRow like:

<script language="JavaScript">
    function *DoubleClickRow*(thisRow)
    {
         var id = thisRow.ID //Get ID from a row with double click event but it does not shown on table.
         location.href="~/Employees/Details/ + "id" //Open a new page to display employee detail with id above.
    }
</script>

And a table like:

    <table class="table">
    <tr>
        <th>
            <p>Select</p>
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Middle)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DoB)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Gender)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.StartDate)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) { //Data
    <tr ondblclick="*DoubleClickRow*(this)">
        <td>
            <input type="checkbox" />
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Middle)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DoB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Gender)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.StartDate)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.EmployeeId })
        </td>
    </tr>
}
</table>

But It does not work. Please show my mistake, and help me to fix it.

Upvotes: 2

Views: 106

Answers (2)

itzmukeshy7
itzmukeshy7

Reputation: 2677

You can set id something like this:

<tr ondblclick="*DoubleClickRow*(this)" id="@Html.DisplayFor(modelItem => item.ID)">

Now you can get id like this

function *DoubleClickRow*(thisRow){
  var id = thisRow.id;
  /* your code */

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

Pass the reference to this row itself

replace

ondblclick="DoubleClickRow(**item**)"

with

ondblclick="DoubleClickRow(this)"

and DoubleClickRow method as

function DoubleClickRow(thisRow)
{
    var columns = thisRow.children;
    for (var counter = 0; counter < columns.length; counter++ )
    {
       console.log("column " + counter + " = " + columns[counter].innerHTML );
    }
}

Upvotes: 3

Related Questions