将删除ActionLink更改为按钮

时间:2012-09-17 19:49:13

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

我读过你不应该使用链接进行删除。如何将其更改为按钮?

 @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' />

        }

2 个答案:

答案 0 :(得分:3)

  

我读过你不应该使用删除链接。

这是正确的。

  

如何将其更改为按钮?

使用带有提交按钮的表单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之外生成这些表单,以避免表单嵌套。

答案 1 :(得分:0)

您也可以使用锚标记来删除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" })