您好我是MVC的新手,我在隐藏的复选框更改事件中隐藏了通过jQuery填充的字段。
是否有可能获得此值并在MVC @Html.ActionLink
中使用它?
示例代码:
<div class="col-md-3" style="text-align: center;">
<label style="text-decoration: underline;">Quote 3</label>
<br />
<label id="lblQuote3"></label>
<input type="hidden" id="hdfQuote3" />
</div>
<div class="col-md-3 compareButtonDiv" style="text-align: center;">
@Html.ActionLinkNonEncoded("Compare Risks", "CompareRisks", "Lead", routeValues: new { bid = @Request.QueryString.Get("bid") }, htmlAttributes: new { @class = "btn btn-primary btn-md", @title = "Compare Risks" })
</div>
我试图从隐藏字段lblQuote3
获取值并将该值发送给控制器。我想将我包含的值发送到rootValues
。
答案 0 :(得分:0)
在接受Stephen Muecke的建议后,我能够为我的问题建立解决方案。
在我的页面上,我动态添加了一个复选框,当它们的checked属性发生更改时,调用一个函数来向/ <label>
添加或删除文本。
然后将此文字添加到href
的{{1}}或从button
移除。
我想出的解决方案是编写一些函数来处理我需要的所有逻辑。
Checkbox On Change事件:
$('input[type="checkbox"].style2').checkbox().on('change', function () {
var compareRiskLinkObject = $('#lnkCompareRisks');
var compareRiskLinkHref = compareRiskLinkObject.attr('href');
var QuoteRef = "";
var checkboxID = $(this).attr('id');
QuoteRef = $(this).siblings('#hdfQuoteRef').val();
if ($(this).prop('checked')) {
checkAndStoreToAvailableRiskSlot(QuoteRef, checkboxID, compareRiskLinkObject, compareRiskLinkHref);
} else {
checkAndRemoveFromRiskSlot(QuoteRef, compareRiskLinkObject, compareRiskLinkHref);
}
});
调用的函数:
function checkAndStoreToAvailableRiskSlot(quoteRef, checkboxID, linkbutton, href) {
// get IDs of 3 risk slots
var risk1 = $('#lblQuote1');
var risk2 = $('#lblQuote2');
var risk3 = $('#lblQuote3');
var hdf1 = $('#hdfQuote1');
var hdf2 = $('#hdfQuote2');
var hdf3 = $('#hdfQuote3');
if (risk1.text() == "") {
risk1.text(quoteRef);
hdf1.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk1=" + quoteRef);
} else if (risk1.text() != "" && risk2.text() == "") {
risk2.text(quoteRef);
hdf2.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk2=" + quoteRef);
} else if (risk1.text() != "" && risk2.text() != "" && risk3.text() == "") {
risk3.text(quoteRef);
hdf3.val(checkboxID);
appendHrefWithRisks(linkbutton, href, "risk3=" + quoteRef);
} else {
alert("You have already selected 3 quotes. Please uncheck one before adding another.");
}
}
function checkAndRemoveFromRiskSlot(quoteRef, linkbutton, href) {
// get IDs of 3 risk slots
var risk1 = $('#lblQuote1');
var risk2 = $('#lblQuote2');
var risk3 = $('#lblQuote3');
if (risk1.text() === quoteRef) {
risk1.text("");
removeFromHref(linkbutton, "risk1", href);
} else if (risk1.text() !== quoteRef && risk2.text() === quoteRef) {
risk2.text("");
removeFromHref(linkbutton, "risk2", href);
} else if (risk1.text() !== quoteRef && risk2.text() !== quoteRef && risk3.text() === quoteRef) {
risk3.text("");
removeFromHref(linkbutton, "risk3", href);
} else {
alert("You have already selected 3 quotes. Please uncheck one before adding another.");
}
}
function appendHrefWithRisks(linkbutton, href, appendData) {
var newHref = "";
newHref = href + '&' + appendData;
linkbutton.attr('href', newHref);
}
function removeFromHref(linkbutton, risk, href) {
var newHref = "";
if (risk == "risk1") {
newHref = href.replace(/&?((risk1))=[^&]*/gi, "");
} else if (risk == "risk2") {
newHref = href.replace(/&?((risk2))=[^&]*/gi, "");
} else if (risk == "risk3") {
newHref = href.replace(/&?((risk3))=[^&]*/gi, "");
}
linkbutton.attr('href', newHref);
}
如果选中复选框,则会调用function checkAndStoreToAvailableRiskSlot
,然后调用function appendHrefWithRisks
。
链接按钮的href
将通过这些方法传递,然后在成功向标签添加风险时附加。
希望有一天能帮助别人:)