[英]Having problems with the JavaScript/jquery and c# implementation
I am following a 3 year old c# course on udemy and am unable to solve the problem regarding the implementation of javascript/jquery code.我正在关注 udemy 上的 3 岁 c# 课程,但无法解决有关 javascript/jquery 代码实现的问题。 I am suppose to connect the registration code (via jquery/javascript) with a subscription, however my textbox does not work regardless of the information I insert in it, it is not displaying any text, and it should be displaying different messages such are - Could not register the code., The code was successfully added.
我想将注册码(通过 jquery/javascript)与订阅连接起来,但是无论我在其中插入什么信息,我的文本框都不起作用,它不显示任何文本,它应该显示不同的消息,例如 -无法注册代码。,代码已成功添加。
Here is my code:这是我的代码:
RegisterCodeController :注册代码控制器:
public class RegisterCodeController : Controller
{
public async Task<ActionResult> Register(string code)
{
if(Request.IsAuthenticated)
{
var userId = HttpContext.GetUserId();
var registred = await SubscriptionExtension.RegisterUserSubscriptionCode(userId, code);
if (!registred) throw new ApplicationException();
return PartialView("_RegisterCodePartial");
}
return View();
}
_RegisterCodePartial : _RegisterCodePartial :
@if(Request.IsAuthenticated)
{
<div class="panel panel-primary register-code-panel">
<div class="panel-heading">
<h4 class="panel-title">Register Code</h4>
</div>
<div class="panel-body input-group">
<input type="text" class="form-control" placeholder="Enter code here..." />
<button class="btn btn-primary input-group-btn" type="button" >Submit
<h3 class="panel-title">Submit</h3>
</button>
</div>
<div class="alert alert-danger hidden">Could not register code</div>
</div>
}
RegisterCode.js注册码.js
$(function () {
var code = $(".register-code-panel input");
function displayMessage(success, message)
{
var alert_div = $(".register-code-panel .alert");
alert_div.txt(message);
if (success)
alert_div.removeClass('alert-danger').addClass('alert-success');
else
alert_div.removeClass('alert-success').addClass('alert-danger');
alert_div.removeClass('hidden');
}
$(".register-code-panel button").click(function (e) {
$(".register-code-panel .alert").addClass('hidden');
if (code.val().length == 0)
{
displayMessage(false, "Enter a code");
return;
}
$.post('/RegisterCode/Register', { code: code.val() },
function (data)
{
displayMessage(true, "The code was successfully added. /n/r Please reload the page.");
code.val('');
}).fail(function (xlr, status, error) {
displayMessage(false, "Could not register the code.");
});
});
});
You are making a POST
request but looking at your controller action method it looks like a GET
.您正在发出
POST
请求,但查看您的 controller 操作方法,它看起来像GET
。 Thus change your API call $.post('/RegisterCode/Register', { code: code.val() },
to $.get('/RegisterCode/Register', { code: code.val() },
probably and use a HTTP verb explicitly in your action method因此,将您的 API 调用
$.post('/RegisterCode/Register', { code: code.val() },
更改为$.get('/RegisterCode/Register', { code: code.val() },
可能和在您的操作方法中明确使用 HTTP 动词
[HttpGet]
public async Task<ActionResult> Register(string code)
{
}
Solved, I had the RegisterCode.css file with the same name as RegisterCode.js file, it seems to me this caused some confusion while retrieving data in the Bundle.config, so I renamed the.js file and now it works!解决了,我有一个与 RegisterCode.js 文件同名的 RegisterCode.css 文件,在我看来,这在 Bundle.config 中检索数据时引起了一些混乱,所以我重命名了 .js 文件,现在它可以工作了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.