单击按钮上的“调用操作方法”,单击ASP.NET MVC

时间:2019-09-09 06:16:45

标签: 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);
        }

在我的视图中,我关注了

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

2 个答案:

答案 0 :(得分:1)

  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核心开发,则可以尝试

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

答案 1 :(得分:0)

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

date

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

URL调用也需要修改

for date in start_dates:
    date += dt.timedelta(days=1)
    print(date)