我是Asp.Net MVC的新手。我正面临这个问题,我在视图文件中强烈输入了输入字段。当我向控制器acion-method发送ajax调用时,模型对象不会获得任何值。我的代码已附上。
查看代码
@model Swift_MT_103.Models.MT102_Dynamic
@{
ViewBag.Title = "MT102Appl";
}
@Html.TextBoxFor(m=>m.accountNo_50k_1, "accountNo_50k_1", new { @class="form-control",@name= "accountNo_50k_1", @id= "accountNo_50k_1" })
@Html.TextBoxFor(m => m.accountTitle_50k_2, "accTitle", new {@name = "accTitle", @id = "accountTitle1"})
@Html.TextBoxFor(m => m.branchCode_50k_3, "branchcode", new { @name = "accountNo", @id = "branchcode1"})
@Html.TextBoxFor(m => m.misc_150k_4, "MiscInfo1", new { @class = "form-control",@name = "MiscInfo1", @id = "MiscInfo1" })
@Html.TextBoxFor(m => m.misc_250k_5, "MiscInfo2", new { @class = "form-control"@name = "MiscInfo2", @id = "MiscInfo2" })
@Html.TextBoxFor(m => m.accountNo_59_1, "BenAccNo", new { @class = "form-control" @name = "BenAccNo", @id = "BenAccNo" })
@Html.TextBoxFor(m => m.accountTitle_59_2, "BenAccTitle", new { @class = "form-control", @name = "BenAccTitle", @id = "BenAccTitle" })
@Html.TextBoxFor(m => m.branchCode_59_3, "BenBranchCode", new { @class = "form-control"@name = "BenBranchCode", @id = "BenBranchCode" })
@Html.TextBoxFor(m => m.misc_159_4, "BenMiscInfo1", new { @class = "form-control",@name = "BenMiscInfo1", @id = "BenMiscInfo1" })
@Html.TextBoxFor(m => m.misc_259_5, "BenMiscInfo2", new { @class = "form-control", @name = "BenMiscInfo2", @id = "BenMiscInfo2" })
按钮代码
<button type="button" class="btn btn-success" id="add">Add</button>
jQuery代码
$("#add").click(function () {
$.ajax({
type: "POST",
url: "/MT102/SaveData",
contentType: "application/json; charset=utf-8",
dataType: "json",
});
});
控制器代码
MT102_Dynamic obj = new MT102_Dynamic();
[HttpPost]
public void SaveData(MT102_Dynamic model) {
var tmp = obj.accountNo_50k_1;
}
当我使用调试器并尝试观察值时,它只显示空值。需要帮助。
答案 0 :(得分:0)
您的ajax代码没有发布到服务器上。您可以在表单标记(@Html.BeginForm
)中包含控件,也可以提交表单,否则从jQuery读取值并使用您的ajax发布。
参见ajax示例。
var task = {
ProjectId: $('#ddlProjects').val(),
TaskNumber: $('#txtTaskNumber').val(),
};
$.ajax({
url: '/Tasks/UpdateTask',
type: 'POST',
data: task,
success: function (result) {
// success result here
},
error: function () {
// error result here
}
});
答案 1 :(得分:0)
在你的Ajax中你没有传递数据,使用它会帮助你
var req = {
accountNo_50k_1: "50", //The property name
branchCode_50k_3:"300"
}
$.ajax({
url: '/MT102/SaveData', //Hope Your URL Is Correct
type: 'POST',
data: req,
dataType: "json",
// async: true, By default, all requests are sent asynchronously
success: function (result) {
if (result != null) {
}
},
error: function (e) {
}
});