【问题标题】:Call Action Method on button click ASP.NET MVC在按钮单击 ASP.NET MVC 时调用操作方法
【发布时间】:2020-01-10 21:47:04
【问题描述】:

我正在尝试在按钮 click 中获取用户输入。 当用户插入数字并按下Check时,需要返回xml数据类型。 所以在我的controller 中,我创建了function,它将返回一个用于传递ID 的数据

[ResponseType(typeof(AKONTA))]
        public IHttpActionResult GetAKONTA(string id)
        {
            AKONTA aKONTA = db.AKONTAS.Find(id);
            if (aKONTA == null)
            {
                return BadRequest("Ne postoji A_KONTO pod tim rednim brojem");
            }

            return Ok(aKONTA);
        }

在我的视图中,我有关注

<br /><br />
<form>
    <div class="form-group">
        <label>A_KONTO</label>
        <input type="text" class="form-control" aria-describedby="AKONTO BROJ" placeholder="Unesite broj AKONOTO">
    </div>

    <div class="form-group">
        <a asp-action="Index" class="btn btn-primary" id="aKonto" action="@Url.Action("GetAKONTA", "Akontas")">Provjeri</a>
    </div>
</form>

我想在btnclick中创建,当用户传递ID它需要返回XML数据格式。

到目前为止,我创建了一个JS 函数,但我不知道JavaScript,也不知道如何将Controller Action Result 传递给JS 的逻辑。

  <script>
        $(document).ready(function () {
            $('#aKonto').click(function () {
                document.getElementById("aKonto").onclick = function () {GetAKONTA()};;
            });
        });
    </script>

如果有人可以帮助我,我将非常感激。 干杯!

更新

function aKontoSubmit() {
            $.ajax({
                type: "GET",
                url: 'api/Akontas',
                //data: { id: id },
                dataType: "xml",
                success: function (result) {
                    // action to do after form submit
                },
                error: function () {
                    alert("Ne postoji AKONTO pod tim rednim brojem");
                }

            });
        }

**routeConfig**

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace AkontasWebApi
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

【问题讨论】:

  • 你在使用 MVC 5 吗?
  • 是的,ASP.NET MVC5
  • 你不能在表单本身中使用 post 方法
  • 不,因为我需要在我的控制器中调用 JS 中的函数。它需要使用 btn click 方法。如果您有任何建议,我将非常感谢

标签: javascript asp.net-mvc asp.net-web-api


【解决方案1】:

经过几个小时的调试和搜索,我发现我忘记放了

window.location.href = "http://localhost:57285/api/Akontas/" + $('#AkontasId').val();

如果数据库中存在项目,则该位置应重定向

URL 调用也需要修改

URL: "/api/Akontas/GetAKONTA",


 function aKontoSubmit() {

        $.ajax({          
            type: "GET",            
            URL: "/api/Akontas/GetAKONTA",
            data: { id: $('#AkontasId').val() },
            contentType: "data/xml; charset=utf-8",  
            success: function (result) {
                window.location.href = "http://localhost:57285/api/Akontas/" + $('#AkontasId').val();
            },
            error: function () {
                alert("Ne postoji AKONTO pod tim rednim brojem");
            }
        });
    }

【讨论】:

    【解决方案2】:
    1. 添加Jquery的引用,尝试ajax调用方式。

      function aKontoSubmit() {
      $.ajax({
          type: "POST",
          url: '/Akontas/GetAKONTA',
          data: $('form').serialize(),
          dataType: "json",
          success: function (result) {
              // action to do after form submit
          },
          error: function () {
              alert("Error while inserting data");
          }
      });
      

      }

      1. 如下更改链接代码
     <a asp-action="Index" class="btn btn-primary" id="aKonto"  onClick='return aKontoSubmit() '>Provjeri</a>
    

    或者如果你使用的是 ASP.Net MVC Core Development 也可以试试

    <form asp-action="GetAKONTA" asp-controller="Akontas" method="post"> 
        <div class="form-group">
            <label>A_KONTO</label>
            <input type="text" class="form-control" aria-describedby="AKONTO BROJ" placeholder="Unesite broj AKONOTO">
        </div>
    
        <div class="form-group">        
             <input class="btn btn-primary" id="aKonto"  type = "submit" value = "Provjeri" /> 
        </div>
    </form>
    

    【讨论】:

    • 我收到错误 Uncaught SyntaxError: Unexpected end of input Uncaught ReferenceError: aKontoSubmit is not defined at HTMLAnchorElement.onclick (Index:43)
    • @ArualPrasth 按检查按钮时出现错误加载资源失败:服务器已响应
    • @Xerror - 尝试更改上面更新的 ajax URL。您还可以发布您尝试过的整个代码吗?
    • 不起作用。似乎 url 需要是 /api/Akontas/{id} 例如这样:localhost:57285/api/Akontas/516005
    • 我相信路由有问题
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    相关资源
    最近更新 更多