程序执行以下步骤:
但是没有任何效果。当我将所有内容放在一个<script>
标签中时,第一个按钮也停止工作。帮助
问题在于,即使buttonSetAddress按钮也无法执行该功能。只有从<script>
<div class="form-group">
<label>Адресс</label>
<input class="form-control" id="AddressInput" type="text" placeholder="@ViewBag.User.Address" readonly /><br />
<button type="button" class="btn btn-primary" id="buttonSetAddress">Изменить</button>
<button type="button" class="btn btn-success" id="buttonSaveAddress" onclick="SaveAddress()" hidden>Сохранить</button>
</div>
js和ajax
<script type="text/javascript">
document.getElementById('buttonSetAddress').onclick = function AddressInputSet() {
document.getElementById('AddressInput').removeAttribute('readonly');
document.getElementById('buttonSetAddress').hidden = 'true';
document.getElementById('buttonSaveAddress').removeAttribute('hidden');
alert('sdfgdfg');
};
document.getElementById('buttonSaveAddress').onclick = function SaveAddress() {
var data = JSON.stringify( {
userId: @ViewBag.User.Id,
address: document.getElementById('AddressInput').value
});
$.ajax({
type: 'POST',
url: '/Account/ResetAddress',
data: data,
dataType: JSON,
success: function () {
document.getElementById('buttonSaveAddress').hidden = 'true';
},
error: function () {
alert('Error');
}
});
};
</script>
控制器
[HttpPost]
public async void ResetAddress(string userId, string address)
{
var user = await _userManager.FindByIdAsync(userId);
user.Address = address;
await _userManager.UpdateAsync(user);
}
答案 0 :(得分:0)
尝试将引号添加到该值
var data = {
userId: '@ViewBag.User.Id', // here
address: document.getElementById('AddressInput').value
};
答案 1 :(得分:0)
var data =JSON.stringify( {
userId: '@ViewBag.User.Id', // here
address: document.getElementById('AddressInput').value
});
您必须使用JSON.stringify将对象转换为json。
而且您还必须在ajax中设置内容类型
答案 2 :(得分:0)
contentType
是您要发送的数据类型,因此application/json; charset=utf-8
是常见的数据,application/x-www-form-urlencoded; charset=UTF-8
也是如此,这是默认设置。
dataType
是您期望从服务器返回的内容:json,html,文本等。jQuery将使用它来找出如何填充成功函数的参数。
由于您的操作返回了void
,因此无法将dataType
设置为JSON。
尝试使用下面的代码,效果很好(请确保网址正确)
<div>
<label>Адресс</label>
<input class="form-control" id="AddressInput" type="text" placeholder="@ViewBag.User.Address" readonly /><br />
<button type="button" onclick ="AddressInputSet()"class="btn btn-primary" id="buttonSetAddress">Изменить</button>
<button type="button" onclick="SaveAddress()" class="btn btn-success" id="buttonSaveAddress" hidden>Сохранить</button>
</div>
@section Scripts{
<script type="text/javascript">
function AddressInputSet() {
document.getElementById('AddressInput').removeAttribute('readonly');
document.getElementById('buttonSetAddress').hidden = 'true';
document.getElementById('buttonSaveAddress').removeAttribute('hidden');
alert('sdfgdfg');
};
function SaveAddress() {
var data ={
userId: @ViewBag.User.Id,
address: document.getElementById('AddressInput').value
};
$.ajax({
type: 'POST',
url: '/Account/ResetAddress',
data: data,
//contentType: "application/x-www-form-urlencoded; charset=utf-8",
//dataType: JSON,
success: function () {
document.getElementById('buttonSaveAddress').hidden = 'true';
},
error: function () {
alert('Error');
}
});
};
</script>
}
操作:
[HttpPost]
public async void ResetAddress(string userId, string address)