javascript - 在按钮上调用操作方法单击 ASP.NET MVC

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

我正在尝试让用户在按钮 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);
        }

在我的 View 中,我有以下内容

<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>

我想在 btn 中创建 click 当用户传递 ID 它需要返回 XML 数据格式.

IMAGES

到目前为止,我创建了一个 JS 函数,但我不知道 JavaScript 也不知道如何传递 Controller Action ResultJS

  <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 }
            );
        }
    }
}

最佳答案

  1. 添加Jquery的Reference,试试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>

关于javascript - 在按钮上调用操作方法单击 ASP.NET MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848709/

相关文章:

javascript - 如何在水平滑动器(idangero swiper)中启用垂直滚动?

javascript - 按javascript中的多个对象属性对数组进行排序

asp.net-mvc - Entity Framework 和asp.net mvc

c# - 自定义 json contractresolver .net web api 在某些情况下回落到 camelcase

c# - Web API 脚手架 + MVC View

c# - 获取json数据和图片

javascript - parse.com - 如何在电子邮件中生成和添加验证 url?

java - 如何定义谷歌地图中区域外的一个点

asp.net-mvc - Visual Web Developer Express 中的 SubSonic ASP.NET MVC 示例

c# - ASP.NET MVC DropDownList 在事件时不显示颜色