Javascript 视图未将模型传递给控制器-ASP.Net MVC
这是从视图到控制器的基本值传递,但这并不起作用。当我单击更新按钮更新数据库中的记录时,视图中的值不会正确地将值传递给控制器。在javascript中加入调试器后,每个变量都能够正确地获取其值,并将evn存储到对象中 这个问题的可能原因是什么 下面是Javascript中单击事件代码的按钮Javascript 视图未将模型传递给控制器-ASP.Net MVC,javascript,c#,asp.net,asp.net-mvc,Javascript,C#,Asp.net,Asp.net Mvc,这是从视图到控制器的基本值传递,但这并不起作用。当我单击更新按钮更新数据库中的记录时,视图中的值不会正确地将值传递给控制器。在javascript中加入调试器后,每个变量都能够正确地获取其值,并将evn存储到对象中 这个问题的可能原因是什么 下面是Javascript中单击事件代码的按钮 $('#updatePrescription').click(function () { debugger; ValidateFields(); var dru
$('#updatePrescription').click(function () {
debugger;
ValidateFields();
var drugListIsEmpty = CheckDrugList();
var error = $(".text-danger").length;
if (error == 0 && !drugListIsEmpty) {
debugger;
var prescription = [];
var template = {};
template.templateName = $("#prescriptionTemplateName").val();
template.templateTypeId = $('input[name=templateTypeId]:checked').val();
template.prescriptionTemplateItemList = [];
template.instructionId = $('.instruction').val();
template.frequencyId = $('.frequency').val();
template.day = $('.inputDays').val();
template.quantity = $('.inputQuantity').val();
template.dispenseLocationId = $('.selectDispenseLocation').val();
template.statusId = $('.status').val();
//template.categoryId = $('.templateCategory').filter(":visible").last().val();
template.templateId = $('#prescriptionTemplateId').val();
//if (template.categoryId == null) {
// template.categoryId = 0;
//}
var x = 0;
$('#tblPrescriptionSaveTemplateBody tr').each(function (key, value) {
debugger;
var row = $(this).closest('tr');
var next_row = $(row).next();
var drugId = $(value).find('.drugId').val();
var dosage = $(value).find('.inputDosage').val();
var dosageUnitId = $(value).find('.selectUnitId').val();
var statusId = "41";
var remarks = $(value).find('.inputDescription').val();
var groupId = $(value).find('.inputGroupNo').val();
var unit = $(value).find('.selectUnitId').val();
var prescriptionTemplateItemId = $(value).find('.prescriptionTemplateItemId').val();
x++;
var obj = {
// templateId: prescriptionTemplateId,
prescriptionTemplateId: template.templateId,
prescriptionTemplateItemId: prescriptionTemplateItemId,
drugId: drugId,
dosage: dosage,
dosageUnitId: dosageUnitId,
instructionId: template.instructionId,
frequencyId: template.frequencyId,
day: template.day,
quanitity: template.quantity,
unit: unit,
remarks: remarks,
dispenseLocationId: template.dispenseLocationId,
groupId: groupId,
statusId: template.statusId
}
template.prescriptionTemplateItemList.push(obj);
//prescription.push(obj)
})
$.ajax({
type: 'POST',
url: '/WestMedicinePrescriptionTemplate/UpdateTemplate',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(template),
success: function (data) {
ShowNotificationMessage(data.notification);
window.location.href = '/WestMedicinePrescriptionTemplate/Index';
}
});
}
});
这将在控制器的参数newtemplate中传递模型的结果,但结果为null
public ActionResult UpdateTemplate([FromBody] PrescriptionTemplateVM newtemplate)
{
int empId = Convert.ToInt32(HttpContext.Session.GetInt32("EmployeeId"));
var notif = "Update Failed.";
try
{
if (ModelState.IsValid)
{
bool updateSuccessful = _prescription.UpdatePrescriptionTemplateAndItems(newtemplate, empId);
if (updateSuccessful)
{
notif = "Update Successful.";
}
}
}
catch (Exception ex)
{
notif = ex.Message;
}
return Json(new { notification = notif });
}
代码中可能存在什么问题?请执行以下操作:
[HttpPost]
public ActionResult UpdateTemplate(PrescriptionTemplateVM newtemplate)
您需要确保使用的变量名称与在PrescriptionTemplateVM中定义的相同
不要将数据转换为Json。这样做:
$.ajax({
type: 'POST',
url: '/WestMedicinePrescriptionTemplate/UpdateTemplate',
dataType: 'json',
contentType: 'application/json',
data: {newtemplate: template},
success: function (data) {
ShowNotificationMessage(data.notification);
window.location.href =
'/WestMedicinePrescriptionTemplate/Index';
}
});
将[HttpPost]添加到控制器操作以启动。不要将模板变量转换为JSON字符串。直接将其设置为data:data:template。