【发布时间】:2018-12-27 12:19:10
【问题描述】:
我正在尝试将 MVC 模型对象从 JavaScript 传递给控制器操作,但是当我已经尝试了一天多时,它要么是 null 要么是错误。
Controller 参数 JC 始终为 null。
我的 JavaScript 代码:
$(function() {
$(".dialog-modal").dialog({
height: 140,
modal: true
});
});
$(function() {
$("#job-save").dialog({
height: 240,
modal: true
});
});
$("#job-save").dialog({
close: function(event, ui) {
window.location.href = "/EquipmentAll";
}
});
$(function() {
$("#ContactKey").change(function() {
var t = $(this).val();
if (t !== "") {
$.post("@Url.Action("GetContactDefaults")?val=" + t + "&JC=" + @Model,
function(res) {
if (res.Success === "true") {
//enable the text boxes and set the value
$("#ContactEmail").prop('disabled', false).val(res.Data.EmailAddress);
$("#Height").prop('disabled', false).val(res.Data.Height);
} else {
alert("Error getting data!");
}
}
)
} else {
//Let's clear the values and disable :)
$("input.editableItems").val('').prop('disabled', true);
}
});
});
我的控制器动作代码是:
[HttpPost]
public ActionResult GetContactDefaults(string val, JobCreate JC)
{
//Contact tContact = JC.ContactList.FirstOrDefault(c => c.ContactKey == val);
if (val != "")
{
// Values are hard coded for demo. you may replae with values
// coming from your db/service based on the passed in value ( val.Value)
return Json(new { Success = "true", Data = new { EmailAddress = "col@ss", TelAreaCode = 345, TelNo = 100, ACa = "444" } });
}
return Json(new { Success = "false" });
}
【问题讨论】:
标签: javascript model-view-controller model controller