没有将ID从使用Ajax创建的表行中的按钮传递给下一个控制器

没有将ID从使用Ajax创建的表行中的按钮传递给下一个控制器

问题描述:

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

I have a form and when i click on search button, i want the data to be displayed in the table without reloading the page. Here i'm using ajax. I managed to display the rows. Supposedly when i click on a select button in the row, the id of the chosen row will be passed to another page. But i got null error because the id value is null. I also put breakpoints to check the id value and it is null. As though the id is not passed at all.

$('#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的控制器:

Below is the controller of PatientMedicalInfo:

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

我也尝试过在同一页面中传递ID,但这也不起作用.谁能帮我..告诉我我做错了吗?

I also tried passing the id in the same page, but it doesn't work either. Can anyone help me please.. tell me what i'm doing wrong?

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

@Url.Action() is razor code and is parsed on the server before its sent to the view. Values for data[i].paid do not even exist at the point.

此外,PatientMedicalInfo()是GET,因此无需生成form标记,并且由于<button>

In addition, PatientMedicalInfo() is a GET so there is no need to generate a form tag, and your generating invalid html because of the duplicate id attributes in your <button>

您可以简单地生成

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

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

and then add the following script to handle the buttons .click() event to redirect

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