我正在尝试让用户在按钮 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
数据格式.
到目前为止,我创建了一个 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 }
);
}
}
}
最佳答案
添加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"); } });
- 如下更改您的链接代码
<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/