如何使用.cshtml(Razor)页面中的循环参数调用JavaScript函数?

时间:2016-06-24 10:05:59

标签: javascript razor asp.net-mvc-5

我的cshtml视图中有一个循环:

@foreach (var ph in Model.Item2)
{
    <tr>
        <td>@ph.Id</td>
        <td>@ph.Name</td>
        <td>
            <input type="button" class="rounded-button rounded-button-blue" value="@Resources.Language.edit" onclick="location.href='@Url.Action("Edit", "PublishingHouse", new { Id = @ph.Id })'" />
        </td>
        <td>
            <input type="button" id="remove" class="rounded-button rounded-button-blue" value="@Resources.Language.delete" onclick="location.href='@Url.Action("Remove", "PublishingHouse", new { Id = @ph.Id })'"  />
        </td>
    </tr>
}

我想调用一个JavaScript函数,它将显示bootbox.Confirm / Alert窗口,然后调用controller方法。我尝试了很多不同的方法,但点击按钮后没有发生任何事情或按钮刚刚消失。

我尝试了以下其他方法:

尝试1:

onclick="remove(this)" data-assigned-id="@ph.Id"

<script type="text/javascript">
    function remove(elem)
    {
        var id = $(elem).data('assigned-id');
        bootbox.alert("Deleting");
    }
</script>

尝试2:

onclick="@remove('"+ph.Id+"')")"

function remove(phId)
{
    bootbox.confirm("Are you sure you want to delete Publishing House" + phId, function(result)
    {
        if (result == true)
            bootbox.alert("Usuwam");
    });
}

尝试3:

onclick="@("remove('"+ph.Id+"')")"

0 个答案:

没有答案