如何在JavaScript中将对象传递给函数

时间:2016-03-16 10:18:52

标签: javascript asp.net-mvc

我正在完成有关ASP.NET MVC和ASP.NET WEB API的任务 我有数据库:

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

我正在使用EF和ADO.NET建立从数据库到ASP.NET的连接。 我写了一个函数 DoubleClickRow ,如:

<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>

和表格一样:

    <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>

但它不起作用。请出示我的错误,并帮我解决。

2 个答案:

答案 0 :(得分:3)

将引用传递给此行

替换

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

ondblclick="DoubleClickRow(this)"

和DoubleClickRow方法

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

答案 1 :(得分:0)

您可以将ID设置为:

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

现在你可以得到像这样的身份

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