Javascript actionlink上的MVC提示
我找到了确认窗口的代码,但我想创建一个提示窗口,并将用户提供的信息传递给控制器(不知道如何获取值并将其放入RouteValue:/) 它应该是这样工作的:Javascript actionlink上的MVC提示,javascript,asp.net-mvc,Javascript,Asp.net Mvc,我找到了确认窗口的代码,但我想创建一个提示窗口,并将用户提供的信息传递给控制器(不知道如何获取值并将其放入RouteValue:/) 它应该是这样工作的: 用户单击操作链接 提示 控制器从提示符获取值 代码: <%= Html.ActionLink( "Add", "Add", new { id = item.foo}, new { onclick = "return confirm('foo');" }) %> public ActionResult SetOkReject
<%= Html.ActionLink(
"Add",
"Add",
new { id = item.foo},
new { onclick = "return confirm('foo');" }) %>
public ActionResult SetOkReject(int? RequestId,string MSG="")
{
//. . .
}
<a href='#' onclick = 'return Reject(@Model.ElementAt(I).ID)'
> Reject the request</a>
<script>
function Reject(RequestId)
{
var T = confirm(' Do you reject this request?');
if (T)
{
var Prom = prompt("Please enter the reason for
rejecting the request", "");
if (Prom == null || Prom == "") {
// nothing
} else {
var curl = ActRejext + '?RequestId=' + RequestId + '&MSG=' + Prom;
window.location.href = curl;
return true;
}
return true;
}
return false;
}
</script>
如果您只需要一个带有一个参数的提示,可以尝试在此处使用Window prompt(): 如果需要更多参数,可以尝试引导模式:(最后一个示例)Hi
如果您在MVC控件中有如下操作:
<%= Html.ActionLink(
"Add",
"Add",
new { id = item.foo},
new { onclick = "return confirm('foo');" }) %>
public ActionResult SetOkReject(int? RequestId,string MSG="")
{
//. . .
}
<a href='#' onclick = 'return Reject(@Model.ElementAt(I).ID)'
> Reject the request</a>
<script>
function Reject(RequestId)
{
var T = confirm(' Do you reject this request?');
if (T)
{
var Prom = prompt("Please enter the reason for
rejecting the request", "");
if (Prom == null || Prom == "") {
// nothing
} else {
var curl = ActRejext + '?RequestId=' + RequestId + '&MSG=' + Prom;
window.location.href = curl;
return true;
}
return true;
}
return false;
}
</script>
将ActionLink转换为如下标记:
<%= Html.ActionLink(
"Add",
"Add",
new { id = item.foo},
new { onclick = "return confirm('foo');" }) %>
public ActionResult SetOkReject(int? RequestId,string MSG="")
{
//. . .
}
<a href='#' onclick = 'return Reject(@Model.ElementAt(I).ID)'
> Reject the request</a>
<script>
function Reject(RequestId)
{
var T = confirm(' Do you reject this request?');
if (T)
{
var Prom = prompt("Please enter the reason for
rejecting the request", "");
if (Prom == null || Prom == "") {
// nothing
} else {
var curl = ActRejext + '?RequestId=' + RequestId + '&MSG=' + Prom;
window.location.href = curl;
return true;
}
return true;
}
return false;
}
</script>
并在Javascript中编写如下函数:
<%= Html.ActionLink(
"Add",
"Add",
new { id = item.foo},
new { onclick = "return confirm('foo');" }) %>
public ActionResult SetOkReject(int? RequestId,string MSG="")
{
//. . .
}
<a href='#' onclick = 'return Reject(@Model.ElementAt(I).ID)'
> Reject the request</a>
<script>
function Reject(RequestId)
{
var T = confirm(' Do you reject this request?');
if (T)
{
var Prom = prompt("Please enter the reason for
rejecting the request", "");
if (Prom == null || Prom == "") {
// nothing
} else {
var curl = ActRejext + '?RequestId=' + RequestId + '&MSG=' + Prom;
window.location.href = curl;
return true;
}
return true;
}
return false;
}
</script>
函数拒绝(请求ID)
{
var T=confirm(‘您是否拒绝此请求?’);
if(T)
{
var Prom=prompt(“请输入原因
拒绝请求“,”);
如果(Prom==null | | Prom==“”){
//没什么
}否则{
var curl=ActRejext+'?RequestId='+RequestId+'&MSG='+Prom;
window.location.href=curl;
返回true;
}
返回true;
}
返回false;
}
我知道如何使用提示。我只是不明白如何通过动作链接将数据发送给控制器。单击ActionLink时使用提示符后,我将数据发送到Javascript中的一个变量,我不知道如何将该变量传递给控制器。您还可以在google上尝试一些关键词,例如:“将数据从视图传递给控制器”,“通过ajax将数据传递给控制器”…@Valium:您可以使用$.ajax()(post)将数据发送给控制器。我们这里有一个例子:因为,您使用asp。net MVC,您需要一些更改:$.ajax({url:'Controller/Action',type:'POST',data:{para01:value01,para02:value02},success:funnction(){…})注意:para01和para02需要与操作参数的名称相同。