asp.net-mvc 使用 javascript 的 ASP.Net MVC 3.0 Ajax.ActionLink 动态对象路由值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8173150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net MVC 3.0 Ajax.ActionLink dynamic object route values using javascript
提问by HaBo
0 Project
0 项目
In my view I have a hidden filed which has a UserID. This user id is generated upon an action (so this will not be know prior)
在我看来,我有一个隐藏的文件,它有一个UserID. 这个用户 id 是在一个动作上生成的(所以这不会事先知道)
Once this hidden field has a value, I would like to use that value as an actionlink routevalue. Can I do it with a jquery selector.
一旦这个隐藏字段有一个值,我想使用该值作为 actionlink 路由值。我可以用 jquery 选择器来做吗?
My hidden field is
我的隐藏字段是
<input id="NewUserID" type="hidden" value="80">
My ajax.ActionLink is
我的 ajax.ActionLink 是
@Ajax.ActionLink("Edit", "EditUser", "User", new { UserID = "$('#GetNewPatientID').val()" },
new AjaxOptions
{
OnSuccess = "ShowEditUserForm",
UpdateTargetId = "EditUserDetails",
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get"
}, new { @class = "button", id = "EditUserButton" })
Any idea if this is possible?
知道这是否可能吗?
回答by Darin Dimitrov
When generating the action link on the server you could put some special placeholder for the UserID route value:
在服务器上生成操作链接时,您可以为 UserID 路由值放置一些特殊的占位符:
@Ajax.ActionLink(
"Edit",
"EditUser",
"User",
new {
UserID = "__userid__"
},
new AjaxOptions {
OnSuccess = "ShowEditUserForm",
UpdateTargetId = "EditUserDetails",
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get"
},
new {
@class = "button",
id = "EditUserButton"
}
)
and then when you assign a value to the hidden field in javascript you could update the action link href as well:
然后当您为 javascript 中的隐藏字段分配一个值时,您也可以更新操作链接 href :
$('#EditUserButton').attr('href', function() {
return this.href.replace('__userid__', $('#NewUserID').val());
});
回答by Tomasz Jaskuλa
Maybe something like this would work. Put you action link in a div and modify it with jquery on the client side.
也许这样的事情会奏效。将您的操作链接放在一个 div 中,并在客户端使用 jquery 对其进行修改。
<div id="ajaxTest">
@Ajax.ActionLink("Edit", "EditUser", "User", new { UserID = "$('#GetNewPatientID').val()" },
new AjaxOptions
{
OnSuccess = "ShowEditUserForm",
UpdateTargetId = "EditUserDetails",
InsertionMode = InsertionMode.Replace,
HttpMethod = "Get"
}, new { @class = "button", id = "EditUserButton" })
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#ajaxTest a").click(function (event) {
$(this).attr('href', "/EditUser/Edit?UserId='+ $('#NewUserId).val() +'");
});
});
</script>
回答by Atters
The answer provided by Darin is great and helped me, however as the comment suggests if you need to click the link again and pass a different value, how do you do that? This is a requirement if you are updating partial views etc. So here is how I achieved exactly that...
Darin 提供的答案很好,对我有帮助,但是正如评论所暗示的那样,如果您需要再次单击链接并传递不同的值,您会怎么做?如果您正在更新部分视图等,这是一个要求。所以这就是我如何实现的......
$(document).ready(function () {
$('#replyMessageButton').click(function () {
var url = $("#replyMessageButton").attr("href")
$("#replyMessageButton").attr("href", TrimToSlash(url) + $("#MessageId").val())
});
});
function TrimToSlash(value) {
if (value.indexOf("/") != -1) {
while (value.substr(-1) != '/') {
value = value.substr(0, value.length - 1);
}
}
return value;
}
@Ajax.ActionLink("Reply", "ReplyMessage", "MessageBox", new { id = -1 },
new AjaxOptions
{
UpdateTargetId = "replyMessageContainer",
InsertionMode = InsertionMode.Replace,
OnBegin = "UpdateDisplay('replyMessage')",
OnFailure = "UpdateDisplay('default')"
},
new { @id = "replyMessageButton" }
)
Also implemented is a check for messageId > 0 in the controller, hence why the id is initialized to -1. An "Error" view is returned if this condition is not met.
还实现了在控制器中检查 messageId > 0,因此为什么将 id 初始化为 -1。如果不满足此条件,则返回“错误”视图。

