【发布时间】: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