[英]Tried to load table dynamically using AJAX but rows not displayed
在一種形式中,我有一個下拉列表和一個文本框,它將獲取用戶的輸入。 然后,當用戶提交表單時,我不希望整個頁面刷新。 我只希望它下面的表刷新並從數據庫加載數據。 我在下面嘗試了代碼,但僅彈出警告。 該表仍然為空。
$('#frmPrescDrugList').submit(function (e) {
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: '/consultation/PrescDrugList',
type: 'POST',
data: $(this).serialize(), // it will serialize the form data
dataType: 'html',
success: function (data) {
var row_data = "";
row_data += "@{ var rowNum = 0; } @foreach (var item in Model.LoadDrugList) {
rowNum += 1;
<tr Class="odd gradeX">
<td>@rowNum</td>
<td>@item.dsubsystem</td>
<td>@item.tradename </td>
<td style="text-align: center; vertical-align: middle;"><form Class="form-horizontal" role="form" action="@Url.Action("Prescription", "consultation", new {dgid = item.dgid})" method="post"><Button type="submit" Class="btn btn-default" name="btnDrugListSelect">Select</Button></form></td>
</tr>
}";
$("#LoadDrugListTable").append(row_data);
alert('Drug list has been loaded.');
},
error: function () {
alert('Ajax Submit Failed ...');
}
}); // end ajax
}); // end form-submit
LoadDrugListTable是表的ID。 為什么ajax已經成功,為什么不加載數據? 任何人都可以幫助指出我的錯誤或建議我提供更好的示例參考嗎?
我嘗試按照此示例進行操作,但是我陷入了@ Url.Action()的困境。 帶下划線的紅色。 當我嘗試用“”和+分隔時,仍然用紅色強調
字符文字中的字符過多。
更新
這是我提到的未正確傳遞ID的形式。 我將其傳遞給get方法。 順便說一句,這是與我上面提到的頁面完全不同的頁面。 當我嘗試在另一頁中執行相同的操作時,它將傳遞一個空的付費值。 付費是ID。 也許是因為這次我要傳遞到另一個頁面。
"<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>"
這是控制器:
[HttpGet()]
public ActionResult PatientMedicalInfo(string paid)
{
PatientLookupVM Patient = new PatientLookupVM();
dynamic CurrentPatientDetailsByPtId = Patient.GetCurrentPatientDetailsByPtId(paid);
Patient.LoadCurrentPatientDetails = CurrentPatientDetailsByPtId;
return View(Patient);
}
嘗試更換雙"
用單引號'
在行動行情好了一致性,你可以到處替換它們。
<td ... action='@Url.Action("Prescription", "consultation", new {dgid = item.dgid})' ... </td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.