在ontml的html.dropdownlist中发送表中当前项的id

时间:2016-07-13 09:28:06

标签: javascript c# asp.net-mvc razor onchange

我试图通过@onChange将@ item.Id发送到JavaScript函数。但是,当我像下面的代码一样编写它时,JavaScript函数将其作为纯文本读取。

Razor页面:

<table class="table-striped">

    <thead>
        <tr class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <th class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                <i class="fa fa-sort" title="Blocknummer"></i>
            </th>
            <th class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                <i class="fa fa-tag" title="Namn på block"></i>
            </th>
            <th class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                <i class="fa fa-clock-o" title="Längd på block"></i>
            </th>
            <th class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                <i class="fa fa-user" title="Extern aktör"></i>
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.CourseBlocks)
            {
            <tr class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                    @Html.DisplayFor(m => item.BlockOrder)
                </td>
                <td class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                    @Html.DisplayFor(m => item.BlockName)
                </td>
                <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                    @Html.DisplayFor(m => item.DisplayLength)
                </td>
                <td class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                    @if (item.NeedExternActor)
                    {
                        @Html.DropDownListFor(model => item.ExternActorId, new SelectList(Model.ContactPersons, "Value", "Text"), new { @class = "form-control form-control-280 input-sm", @onChange = "actorChange('@item.Id', this.options[this.selectedIndex].value)" })
                    }
                </td>
            </tr>
        }
    </tbody>

</table>

JavaScript的:

function actorChange(id, value) {

var itemId = id;
var actorId = value;
}

提前感谢我能得到的所有帮助。

3 个答案:

答案 0 :(得分:0)

使用

@onChange = "actorChange(@item.Id, this.value)"

答案 1 :(得分:0)

经过新的尝试,所以我通过这样做解决了它:

$dateInputString = "Tue Jul 12 09:48:44 2016";

//Convert the string from the format supplied to a date
$date = DateTime::createFromFormat('D M d H:i:s Y', $dateInputString);

//output the date in the required format
echo $date->format('Y-m-d H:i:s');

在javascript中:

<tbody>
    @foreach (var item in Model.CourseBlocks)
        {
        <tr id="@item.Id" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <td class="col-lg-1 col-md-1 col-sm-1 col-xs-1">
                @Html.DisplayFor(m => item.BlockOrder)
            </td>
            <td class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                @Html.DisplayFor(m => item.BlockName)
            </td>
            <td class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
                @Html.DisplayFor(m => item.DisplayLength)
            </td>
            <td class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
                @if (item.NeedExternActor)
                {
                    @Html.DropDownListFor(model => item.ExternActorId, new SelectList(Model.ContactPersons, "Value", "Text"), new { @class = "form-control form-control-280 input-sm", @onChange = "actorChange(this)" })
                }
            </td>
        </tr>
    }
</tbody>

答案 2 :(得分:0)

然后尝试这个

Now