【问题标题】:How to make delete button an html button instead of an ajax button如何使删除按钮成为 html 按钮而不是 ajax 按钮
【发布时间】:2021-01-04 22:23:00
【问题描述】:

我想将删除按钮从 ajax 按钮更改为 html 按钮。我目前正在使用 ajax/javascript 作为删除按钮。当我点击删除按钮时,会出现一个确认我要删除的模式窗口,一旦我点击确认,它将删除投票。删除操作重定向我更改为 RedirectToAction 但页面错误(InvalidOperationException:没有路由与提供的值匹配。)。我认为重定向不正确,或者我可能需要为该方法再次返回?

  <div class="modal fade" id="delete-vote-modal" tabindex="-1" 
   role="dialog" 
 data-backdrop="static">
  <form method="post" asp-controller="Discussion" asp-action="DeleteVote" 
     enctype="multipart/form-data">
    <input id="packet-item-id" asp-for="@Model.PacketItemId" type="hidden" 
      />
        <input id="voting-type" asp-for="@Model.VotingType" type="hidden" 
     />
     <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Delete Vote</h5>
                <button type="button" class="close" data-dismiss="modal" 
      aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <input type="hidden" name="voteId" id="voteId" value="" />
                Do you want to delete this vote ?
                <div style="text-align: center; display: none" 
            id="loaderDiv">
                    <img src="~/images/loading.gif" width="150" />
                </div>
            </div>
            <div class="modal-footer">
                <a href="#" class="btn btn-default" data- 
              dismiss="modal">Cancel</a>
                <input type="submit"
                       value="Confirm"
                       class="btn btn-success col-sm-12 col-md-2 col-lg-2"
                       data-action="submit" />
                
               </div>
           </div>
        </div>
   </form>
[HttpPost]
    public async Task<IActionResult> DeleteVote(long voteId, int 
    packetItemId, string votingType, CancellationToken cancellationToken)
     {
        try
        {
            await _discussionService.DeleteVoteAsync(
                    voteId,
                    cancellationToken)
                .ConfigureAwait(true);
            TempData.Put(key: TempDataKey.Vote.DeleteMessage,
                
  StatusMessageModel.Create(UserStringsService.VoteDeletedSuccessfully, 
  false));
            return RedirectToAction("/Vote?packetItemId=" + packetItemId + 
    "&votingType=" + votingType);
            //return Json(new { isSuccess = true, statusMessage = 
   UserStringsService.VoteDeletedSuccessfully });
        }
        catch
        {
            Logger.LogError($"Error in deleting the Vote Id: {voteId}");
            TempData.Put(key: TempDataKey.Vote.DeleteMessage,                    
    StatusMessageModel.Create(UserStringsService.VoteDeleteFailed, false));
            return RedirectToAction("/Discussion/Vote?packetItemId=" + 
   packetItemId + "&votingType=" + votingType);
              //return Json(new { isSuccess = false, statusMessage = 
  UserStringsService.VoteDeleteFailed });
        }
      }

【问题讨论】:

    标签: javascript html ajax asp.net-core


    【解决方案1】:

    从代码中,bakend 需要接收带有对象的数据。

    注意:Redirect 中的路由参数是绝对路径。

       [HttpPost]
        public async Task<IActionResult> DeleteVote(ViewModel viewModel, CancellationToken cancellationToken)
        {
            return Redirect("/Discussion/Vote?packetItemId="+viewModel.PacketItemId+ "&votingType="+viewModel.VotingType);
        }
        public IActionResult Vote(int packetItemId, string votingType)
        {
            return Ok(new { packetItemId = packetItemId, votingType = votingType });
        }
    

    型号

    public class ViewModel
    {
    
        public int PacketItemId { get; set; }
        public string VotingType { get; set; }
        //...
    }
    

    【讨论】:

    • 我试过了,它只是提交表单,就好像我投了另一票而不是删除投票一样。我需要删除投票。它的作用就像投票按钮一样。
    • 您想移除投票模块吗?你可以有很多投票卡,所以你可以传递一个对象来找到节点,然后删除元素(投票)。
    • @bootsy,这可以帮助您删除元素。
    • 我根本不想要 ajax。我想使用 html 而根本不使用 ajax。
    • 是的,您可以删除所有 ajax 并通过 bakend 重定向。
    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多