Id 不会从使用 ajax 创建的表行中的按钮传递到下一个控制器



我有一个表单,当我单击搜索按钮时,我希望数据显示在表中而无需重新加载页面。在这里,我使用的是 ajax。我设法显示行。据说当我单击行中的选择按钮时,所选行的 id 将被传递到另一个页面。但是我得到了空错误,因为 id 值为空。我还放置了断点来检查 id 值,它是空的。好像 id 根本没有传递。

$('#btnPatientSearch').on("click", function (e) {
    e.preventDefault(); // Prevent Default Submission
    $.ajax({
        url: '/consultation/GetPatientLookupList',
        type: 'POST',
        data: $('#frmPatientLookup').serialize(), // it will serialize the form data
        dataType: 'json',
        //})
        success: function (data) {
            $('.firstDL').remove();
            $('.subsequentDL').remove();
            var rowNo = 0;
            for (var i = 0; i < data.length; i++) {
                rowNo += 1;
                $("#LoadPatientLookupTable tbody").append("<tr Class='odd gradeX subsequentDL'>" +
                    "<td>" + rowNo + "</td>" +
                    "<td>" + data[i].paid + "</td>" +
                    "<td>" + data[i].pidno + "</td>" +
                    "<td>" + data[i].pidno + "</td>" +
                    "<td>" + data[i].pname + "</td>" +
                    "<td>" + data[i].pgender + "</td>" +
                    "<td style='text-align: center; vertical-align: middle;'><form Class='form-horizontal' role='form' action='@Url.Action("PatientMedicalInfo", "consultation", new { paid = Html.Raw(" + data[i].paid + ") }) method='get'><Button Class='btn btn-default' name='btnSelectPatient' id='btnSelectPatient'>Select</Button></form></td>" +
                    "</tr>");
                    //"<td>" + data[i].ToJavaScriptDate(data[i].pdob) + "</td>" +
            }
            alert('Patient list has been loaded.');
        },
        error: function () {
            alert('Ajax Submit Failed ...');
        }
    }); // end ajax
}); // end btnPatientSearch

以下是PatientMedicalInfo的控制器:

[HttpGet()]
public ActionResult PatientMedicalInfo(string paid)
{
    PatientLookupVM Patient = new PatientLookupVM();
    dynamic CurrentPatientDetailsByPtId = Patient.GetCurrentPatientDetailsByPtId(paid);
    Patient.LoadCurrentPatientDetails = CurrentPatientDetailsByPtId;
    return View(Patient);
}

我也尝试在同一页面中传递 id,但它也不起作用。谁能帮我..告诉我我做错了什么?

@Url.Action()是剃刀代码,在发送到视图之前在服务器上进行分析。data[i].paid的值在该点甚至不存在。

此外,PatientMedicalInfo()是一个 GET,因此无需生成 form 标记,并且由于<button>中的重复id属性而生成无效的 html

您可以简单地生成

'<td><button type="button" class="select" data-id="' + data[i].paid + '">Select</button></td>'

然后添加以下脚本来处理要重定向.click()事件的按钮

var baseUrl = '@Url.Action("PatientMedicalInfo", "consultation")';
$("#LoadPatientLookupTable tbody").on('click', '.select', function() {
    location.href = baseUrl + '?paid=' + $(this).data('id');
});

最新更新