在Asp.net中转发和传递数据
在Asp.net实体框架中,我需要转发到另一个页面,并传递第二个页面处理的一些数据 在PHP中,我可以执行以下操作在Asp.net中转发和传递数据,asp.net,asp.net-mvc,asp.net-web-api,http-post,Asp.net,Asp.net Mvc,Asp.net Web Api,Http Post,在Asp.net实体框架中,我需要转发到另一个页面,并传递第二个页面处理的一些数据 在PHP中,我可以执行以下操作 <!-- page1.php --> <form action="page2.php" method="POST"> <input type="hidden" name="id" /> <input type="submit" value="Go to page 2" /> </form> <!-
<!-- page1.php -->
<form action="page2.php" method="POST">
<input type="hidden" name="id" />
<input type="submit" value="Go to page 2" />
</form>
<!-- page2.php -->
<?php
echo $_POST['id'];
?>
如何在Asp.net中实现这一点
编辑:有一个使用Javascript和jQuery的简单解决方案
<!-- on page 1 -->
$('input[type=submit]').on('click', function (e) {
// Forward to browsing page and pass id in URL
e.preventDefault();
var id= $('input[name=id]').val();
if ("" == id)
return;
window.location.href = "@Request.Url.OriginalString/page2?id=" + id;
});
<!-- on page 2 -->
alert("@Request.QueryString["id"]");
$('input[type=submit]')。在('click',函数(e){
//转发到浏览页面并在URL中传递id
e、 预防默认值();
var id=$('input[name=id]')。val();
如果(“==id)
返回;
window.location.href=“@Request.Url.OriginalString/page2?id=“+id;
});
警报(“@Request.QueryString[“id”]”);
您也可以在ASP.NET中将
与method=“POST”
一起使用。并在代码中获取值:
int id = int.Parse(Request.Form["id"]);
有很多方法可以做到这一点,请看一些指导 HTML页面:
<form method="post" action="Page2.aspx" id="form1" name="form1">
<input id="id" name="id" type="hidden" value='test' />
<input type="submit" value="click" />
</form>
MVC看起来像
@using (Html.BeginForm("page2", "controllername", FormMethod.Post))
{
@Html.Hidden(f => f.id)
<input type="submit" value="click" />
}
@使用(Html.BeginForm(“page2”,“controllername”,FormMethod.Post))
{
@隐藏(f=>f.id)
}
此外,通读这些内容,您不应该盲目地将PHP中的知识转换为ASP.NET MVC,因为您还需要学习MVC模式。至少有两种选择:
会话
(您的第一页)
从会话中获取数据(您的第二页)
http://YOUR_APP/Page2.aspx?id=7
读取第二页中的查询字符串
// First check to see if value is still in session cache
if(Session["Id"] != null)
{
int id = Convert.ToInt32(Session["Id"]);
}
int id = Request.QueryString["id"]; // value will be 7 in this example
在我的项目中,page2是一个cshtml文件。我无法找出其中的C#方法,可以吗?感谢您提供如何在MVC应用程序中发送数据的示例代码。我现在如何从第2页获取发布的id?您的控制器当前是什么样子的?用你得到的更新问题。使用Javascript和jQuery得到了一个更简单的解决方案。无论如何,谢谢你的努力!好的,正如我前面所说的,您应该阅读MVC教程,因为这可能是一个非常有限的解决方案。如果您想将一个完整的对象发布到另一个具有多个属性等的视图,那么该怎么办呢?值得一读MVC中模型绑定的工作原理。
http://YOUR_APP/Page2.aspx?id=7
int id = Request.QueryString["id"]; // value will be 7 in this example