asp.net-mvc - 将 Delete ActionLink 更改为按钮

标签 asp.net-mvc html 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' />

        }

最佳答案

I've read that you shouldn't use a link for deletes.

没错。

How do I change this to a button instead?

使用带有提交按钮的表单,该按钮将 POSTDelete Controller 操作:

@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 Controller 操作:

[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 之外生成这些表单,以避免表单嵌套。

关于asp.net-mvc - 将 Delete ActionLink 更改为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466155/

相关文章:

javascript - 在 html 末尾包含 jquery 和 js 的解决方案,同时包含在中间的控件需要 $(document).ready

html - CSS 内阴影,只有一个 Angular ,没有一侧

asp.net-mvc-3 - 如何访问在 Controller 中创建的 View 中的变量?

c# - 从字符串中的属性创建对象

c# - 在 ASP.NET MVC 5 中从数据库加载 Razor View

c# - 在 ASP.NET MVC 中解析 JSON 值时出错?

html - vs2008 可以禁用自动格式化吗?

C# - ObjectContext 实例已被释放,不能再用于需要连接的操作

javascript - 这个简单的 JavaScript 代码有什么问题?

javascript - 使用 JavaScript/jQuery 创建交互式 map