簡體   English   中英

asp.net 表點擊事件處理程序被調用兩次

[英]asp.net event handler for table click is called twice

我在 asp.net mvc 核心項目中有一個包含一些列的表。

我的視圖文件看起來像這樣

    <h1>Items</h1>
<div class="panel-body">
    <table class="table table-bordered table-responsive table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>Title</th>

                <th>Rating</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Items)
            {
                <tr onclick="location.href='@(Url.Action("ShowItemDetails", "Item", new {Id = item.FilmId}))'">
                    <td>@item.Id</td>
                    <td>@item.Title</td>

                    <td>@item.Rating</td>
                    <td>@Html.ActionLink("Edit", "Edit", "Show", new { id = item.Id })  | @Html.ActionLink("Rate this", "RateItem", new { id = item.Id }) </td>

                </tr>
            }
        </tbody>
    </table>
</div>

問題是當我點擊一行時,controller 方法 ShowItemDetails 被調用了兩次(。)。 我無法從上面的代碼中看到為什么會發生這種情況,此外。 單擊 Edit 或 Rate 這首先調用 ShowItemDetails,然后立即在 controller 中調用 Edit 或 RateItem 方法? 有什么建議可以解決這個問題嗎?

單擊 Edit 或 Rate 這首先調用 ShowItemDetails,然后立即調用 Edit 或 RateItem 方法,因為 Edit 位於表行和表行上,您已調用 showitemdetails 操作。所以,當您單擊 td 時,它首先執行行操作,然后是 td 操作.這就是為什么它被調用兩次。

我希望,您想使用表格數據顯示詳細信息和編輯選項,編輯是 controller 名稱。

調整您的表格代碼,如下所示:

<tbody>
@foreach (var item in Model.Items)
  {
    <tr>
        <td>@Html.ActionLink("Show Details","ShowItemDetails","Item",new {Id = item.FilmId})</td>
        <td>@item.Id</td>
        <td>@item.Title</td>    
        <td>@item.Rating</td>
        <td>@Html.ActionLink("Edit", "Edit", "Show", new { id = item.Id })  | @Html.ActionLink("Rate this", "RateItem", new { id = item.Id }) </td>
    </tr>
  }
</tbody>

問題似乎是由於我認為與 null 圖像無關的東西導致該方法被調用兩次。 設置它來解決這個問題。 需要加代碼檢查 Model.Image != null 很奇怪!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM