[英]Asp.Net Core - pass array value from Controller to view by Json after checkbox is checked?
I have a check box in the view, I want it to return data from the controller when checked, it is displayed in the view and displaying in the input boxes?我在视图中有一个复选框,我希望它在选中时从 controller 返回数据,它显示在视图中并显示在输入框中?
<input class="js-recipient-is-me" type="checkbox"/>
Javascript: Javascript:
$('.js-recipient-is-me').change(function () {
if (this.checked) {
$('.js-input-field').addClass('disabled');
$.ajax({
url: '/Cart/GetUserInfo',
});
} else {
$('.js-input-field').removeClass('disabled');
}
}); });
Html Inputs: Html 输入:
<input type="text" id="mobile" class="js-input-field" name="name" />
<input type="text" id="name" class="js-input-field" name="name" />
<input type="text" id="family" class="js-input-field" name="name" />
Controller: Controller:
public async Task<JsonResult> GetUserInfo()
{
CurrentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var userinfo = await _scope.GetUserInfo(Guid.Parse(CurrentUserId));
return Json(0);
}
'userinfo' has three values 'Mobile-name-family' 'userinfo' 具有三个值 'Mobile-name-family'
I want to put userinfo values into inputs...我想将用户信息值放入输入中......
How is it done?它是如何完成的?
You need to add a callback to handle the data received from the server.您需要添加一个回调来处理从服务器接收到的数据。 You can do this by using done()
after the ajax
request.您可以在ajax
请求之后使用done()
来执行此操作。
$.ajax({
type: "GET",
url: '/Cart/GetUserInfo'
})
.done( function(data) {
$('#mobile').val(data[0]);
$('#name').val(data[1]);
$('#family').val(data[2]);
});
You also need to pass the data from the Controller to the view.您还需要将数据从 Controller 传递到视图。 Currently, it looks like you're always returning 0
.目前,您似乎总是返回0
。 Return the userinfo
object like this: return Json(userinfo);
像这样返回用户信息 object: return Json(userinfo);
userinfo
public async Task<JsonResult> GetUserInfo()
{
CurrentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var userinfo = await _scope.GetUserInfo(Guid.Parse(CurrentUserId));
return Json(userinfo);
}
You should also give your inputs appropriate name
s - they're all sharing name which would cause issues with a regualar form submission, eg: <input type="text" id="mobile" class="js-input-field" name="mobile" />
您还应该为您的输入提供适当的name
- 它们都是共享名称,这会导致常规表单提交出现问题,例如: <input type="text" id="mobile" class="js-input-field" name="mobile" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.