【问题标题】:Change Delete ActionLink to button将 Delete ActionLink 更改为按钮
【发布时间】:2012-09-17 19:49:12
【问题描述】:

我了解到您不应该使用链接进行删除。如何将其更改为按钮?

 @Html.ActionLink("Delete", "Delete", new { id = Model.ListOfRecipients[i].RecipientId,applicationId=Model.ApplicationId },  new { @class = "button delete_recipient" }) 

编辑 @using (Html.BeginForm()) {

        <div id='added_recipients'>

          <table class='header'>
            <tr>
                @if (Model.ListOfRecipients != null)
                {
                <td class='recipient-title'>
                    @if (Model.ListOfRecipients.Count == 1) {@Model.ListOfRecipients.Count.ToString() <span>Recipient</span>}
                    @if (Model.ListOfRecipients.Count > 1)  {@Model.ListOfRecipients.Count.ToString() <span>Recipients</span>}

                </td>
                <td class='express'>
                  Express
                </td>
                <td class='quantity'>
                  Quantity
                </td>
                <td class='action'>
                </td>
                }
            </tr>
          </table>


            @if (Model.ListOfRecipients != null)
            {
                for (int i = 0; i < Model.ListOfRecipients.Count; i++)
                { 

                <div class='recipient-wrapper'>
                    <div class='decision_block'>
                        <table class='recipient'>
                            <tr>
                                <td class='recipient-title'>
                                    @Html.HiddenFor(model=>model.ListOfRecipients[i].RecipientId)
                                    <h3>
                                    @if(Model.ListOfRecipients[i].RecipientTypeId==1)
                                    {
                                        @Html.DisplayTextFor(model => model.ListOfRecipients[i].MedicalLicensingAuthorityName) 
                                        @Html.HiddenFor(model => model.ListOfRecipients[i].MedicalLicensingAuthorityName)                           
                                     }
                                    else
                                    {
                                        @Html.DisplayTextFor(model => model.ListOfRecipients[i].RecipientName) 
                                        @Html.HiddenFor(model => model.ListOfRecipients[i].RecipientName)
                                    }
                                    </h3>
                                    <div class='delivery-type'>
                                        Delivery Type: @Html.DisplayTextFor(model => model.ListOfRecipients[i].DeliveryType)
                                                       @Html.HiddenFor(model => model.ListOfRecipients[i].DeliveryType)
                                    </div>
                                </td>
                                <td class='na express'>
                                    @Html.CheckBoxFor(model => model.ListOfRecipients[i].ExpressIndicator)
                                    @Html.HiddenFor(model => model.ListOfRecipients[i].ExpressIndicator)
                                </td>
                                <td class='quantity'>
                                    <h3>
                                      Qty @Html.DisplayTextFor(model => model.ListOfRecipients[i].Quantity)
                                          @Html.HiddenFor(model => model.ListOfRecipients[i].Quantity)
                                    </h3>
                                </td>
                                <td class='action'>
                                    <input class='button edit_recipient' type='button' value='Edit' />

                                   @* <input class='button delete_recipient' type='button' value='Delete' />*@


                                @Html.ActionLink("Delete", 
                                                 "Delete", 
                                                  new { id = Model.ListOfRecipients[i].RecipientId,applicationId=Model.ApplicationId },  
                                                  new { @class = "button delete_recipient",onlick = "$.post(this.href); return false;" }) 


                                </td>
                            </tr>
                        </table>
                        <div class='recipient_editable'>
                            <br />
                            <hr />


                            <br />
                            <input class='button update_recipient' type='button' value='Update' />
                            <a class='button cancel_update' href='#'>Cancel</a>
                        </div>
                    </div>
                </div>

                }
            }


        </div>

        <input class='button' type='submit' value='Continue' />

        }

【问题讨论】:

    标签: asp.net-mvc html razor asp.net-mvc-4


    【解决方案1】:

    我了解到您不应该使用链接进行删除。

    没错。

    如何将其改为按钮?

    使用带有提交按钮的表单,该按钮将POST 发送到Delete 控制器操作:

    @using (Html.BeginForm("Delete", null, new { id = Model.ListOfRecipients[i].RecipientId, applicationId = Model.ApplicationId }, FormMethod.Post))
    {
       <button type="submit" class="button delete_recipient">Delete</button>
    }
    

    模拟 DELETE HTTP 动词(因为标准 HTML 表单不支持 DELETE):

    @using (Html.BeginForm("Delete", null, new { id = Model.ListOfRecipients[i].RecipientId, applicationId = Model.ApplicationId }, FormMethod.Post))
    {
       @Html.HttpMethodOverride(HttpVerbs.Delete)
       <button type="submit" class="button delete_recipient">Delete</button>
    }
    

    然后使用 HttpDelete 属性装饰您的 Delete 控制器操作:

    [HttpDelete]
    public ActionResult Delete(int id, int applicationId)
    {
        ...
    }
    

    更新:

    现在您已经发布了您的真实代码,您似乎已经在 for 循环之外拥有了一个 HTML 表单。由于无法嵌套表单,因此此解决方案将不起作用。所以一种可能性是使用 AJAX:

    @Ajax.ActionLink(
        "Delete", 
        "Delete", 
        new { 
             id = Model.ListOfRecipients[i].RecipientId,
             applicationId = Model.ApplicationId 
        },
        new AjaxOptions {
            HttpMethod = "DELETE",
            OnSuccess = "function() { alert('The item has been deleted'); }"
        },
        new { 
             @class = "button delete_recipient",
        }
    ) 
    

    如果您不想使用 AJAX,则必须在主 Html.BeginForm 之外生成这些表单,以避免表单嵌套。

    【讨论】:

    • 我无法使用它,因为索引 i 在开始表单元素中无法识别。
    • 这怎么可能?如果它在您的Html.ActionLink 中被识别,它应该在您的Html.BeginForm 中被识别。它们在范围上是严格等价的。我希望你已经将这个Html.BeginForm 包裹在一个带有i 索引的for 循环中。或者,如果您希望获得有用的答案,请提供您的完整代码和确切的错误消息。
    • 现在您已经发布了您的实际代码,请参阅我关于嵌套表单问题的更新答案。
    • 如果方法上没有 [HttpPost] 属性则有效。我也尝试过 [HttpDelete] 并且没有用。但是它没有属性。谢谢
    【解决方案2】:

    您也可以使用锚标记来删除使用httppost 动词。您可以将$.post(this.href); return false; 属性添加到锚标记。更多可以阅读here

        $(".delete_recipient").click(function() {
         $.post(this.href); 
         return false;
        });
    
    @Html.ActionLink("Delete", "Delete", new { id = Model.ListOfRecipients[i].RecipientId,applicationId=Model.ApplicationId },  new { @class = "button delete_recipient" })
    

    【讨论】:

    • 我在哪里添加它?我试过这个@Html.ActionLink("Delete", "Delete", new { id = Model.ListOfTranscriptRecipients[i].RecipientId,applicationId=Model.ApplicationId }, new { @class= "button delete_recipient",onlick = "$.post(this.href); return false;" }) 并且它不起作用
    • 感谢我在控制器删除方法上使用 [HttpPost] 属性。
    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    相关资源
    最近更新 更多