【发布时间】:2022-01-15 03:07:54
【问题描述】:
我有一个按钮,它从模型中获取数据并将其传递给 Ajax 函数。 然后这个函数应该调用一个控制器,但它没有,并且控制器上的断点永远不会被命中。
带有参数的按钮取自Model(都是字符串):
<button class="btn btn-primary btn-lg active" onclick="PassHwData(@obj.Name,@obj.HomeWorldBonus)" >Choose @obj.Name</button>
Ajax 函数:
<script>
function PassHwData(name, hwBonus)
{
$.ajax({
url: '@Url.Action("Create", "HomeWorld")',
type: "POST",
data: {'name' : name, 'hwBonus' : hwBonus}
datatype: "text",
success: function(name, hwBonus)
{
document.getElementById('success').innerHTML += success {name}{hwBonus};
}
})
}
</script>
<div id=success>
success:
</div>
控制器(还有其他方法但我在这里省略了):
using DarkHeresy.Application.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DarkHeresy.Web.Controllers
{
[Authorize]
public class HomeWorldController : Controller
{
private readonly IHomeWorldService _homeWorldService;
public HomeWorldController(IHomeWorldService homeWorldService)
{
_homeWorldService = homeWorldService;
}
[HttpPost]
public async Task<IActionResult> Create(string name, string hwBonus)
{
return View(await _homeWorldService.UpdateCharacterList()); //this will have implementation later
}
}
}
我还应该补充一点,我正在使用 Asp.Net Core MVC 并使用 Clean Architecture,并且在这两方面都是超级新手。
【问题讨论】:
标签: c# html ajax asp.net-mvc asp.net-core