我有一个 View 包含:
(...)
<div id="txtMan@(item.ManufacturerId)" hidden>
@Html.TextBoxFor(modelItem => item.ManufacturerName, new { @id = "txtBoxMan"+item.ManufacturerId })
</div>
<td>
<input class="btnSave" id="btnSave@(item.ManufacturerId)" type="button" value="Save" onclick="saveButtonPressed(@item.ManufacturerId);" hidden />
</td>
(...)
对应的JavaScript:
saveButtonPressed = function (id) {
var newManName = $('#txtMan' + id).val();
$.ajax({
type: "POST",
async: true,
url: '/Admin/BrandConfigurationNameUpdate/' + newManName,
dataType: "json",
success: function () {
alert('Added');
}
});
}
和 Controller :
public static void BrandConfigurationNameUpdate(string id)
{
}
我的目标是将文本框值保存到数据库中。但是,我在我的 Controller 中放置了一个断点,但它从未到达它。有什么建议吗?
编辑: 我已经尝试使用 GetJSON,但仍然无法正常工作。这是代码:
saveButtonPressed = function (id) {
var newManName = $('#txtBoxMan' + id).val();
alert(newManName);
var URL = "~/Areas/Admin/Controller/Admin/BrandConfigurationNameUpdate/";
$.getJSON(URL, { "id": id, "newManName": newManName }, function (data) {
alert("finished");
});
}
最佳答案
您必须将 data
选项作为对象(键值对)传递,否则该值将不会被发布:
saveButtonPressed = function (id) {
var newManName = $('#txtMan' + id).val();
$.ajax({
type: "POST",
async: true,
url: '/AdminController/BrandConfigurationNameUpdate/',
data : {newManName : newManName},
dataType: "json",
success: function () {
alert('Added');
}
});
}
关于javascript - AJAX 调用未将值传递给操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506732/