[英]Pass values from Hidden field to ActionLink then to Controller
我試圖通過讓用戶選擇他們想要的報告的復選框來動態設置 StudentId。 當他們單擊 ActionLink 時,我使用 jQuery 在隱藏字段中設置 id 的值。 我需要操作鏈接來讀取隱藏字段中的 ID。
這些值在隱藏字段中設置,但它們沒有從 actionLink 傳遞到控制器。
我需要將 ReportIds 傳遞給控制器。
@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", new { studentIdList = Model.ReportIds }, new { @class = "button print_selected print_selectedMedEd disabled" })
@Html.HiddenFor(model => model.ReportIds)
javascript
$('.print_selectedMedEd').bind('click', function () {
var ids = getPrintIds();
if (ids.length == 0) {
return false;
}
$('#ReportIds').val(ids.toString());
return true;
});
當 asp.net mvc 呈現您的ActionLink
,它將生成一個帶有模型參數的鏈接。 如果您更改模型的值,它不會更改ActionLink
生成的輸出上的值。
鑒於此,您必須再次設置該值,嘗試生成不帶參數的 ACtionLink:
@Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", null, new { @class = "button print_selected print_selectedMedEd disabled" })
在 javascript 方面,您可以嘗試使用on
方法綁定事件處理程序並從 event 參數調用preventDefault
方法,例如:
$('.print_selectedMedEd').on('click', function (e) {
e.preventDefault();
var ids = getPrintIds();
if (ids.length > 0) {
$('#@Html.IdFor(model => model.ReportIds)').val(ids.toString());
// make an url to redirect
var url = $(this).prop("href") + "?studentIdList=" + ids.toString();
// redirect using the generated url
window.location.href = url;
}
});
請記住使用Html.IdForm()
來確保您擁有特定屬性的正確 id。
那是因為@Html.ActionLink
不使用隱藏字段來發出請求。 操作鏈接呈現后,它會變為<a href="/Student/GetMedEdVerificationReport/reportIds..."> PrintMed </a>
因此您需要修改錨標記上的 html。
您應該使用 Html.Beginform 來傳遞模型。
@using (Html.BeginForm("GetMedEdVerificationReport", "Student", FormMethod.Post, null))
{
@Html.HiddenFor(model => model.ReportIds)
<input type="submit" value="PrintMed" />
}
@Html.HiddenFor(modelItem => item.OrderId)
<td>
<input type="button" value="Pickup" onclick="location.href='@Url.Action("Edit", "Assignment", new { ID = item.OrderId })'" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.