asp.net mvc 2.0 新的RenderAction
环境:windows xp sp3 vs2010 专业版
当然需要说明的是这篇文章来自www.asp.net/mvc 您也可以直接去网站查看这个教程
我们在项目中可能会用到关联的多个表的使用 我们可以用viewdata tempdata传值,同样的强类型也是不错的方法(这里我们不做多的介绍了)
比如我们网站在登录的时候可能需要用户直接注册.然后再进行登录.
我们可能会像下面这样去做:
用户url请求->home/login 然后发现自己未注册 那么会跳转到 home/reg 去注册用户 然后返回到 home/login进行登录
我们可以这样
准备一个homeController 它有Login reg两个action
public ActionResult Login()
{
return View();
}
public ActionResult reg()
{
return View();
}
{
return View();
}
public ActionResult reg()
{
return View();
}
注:这里我就没有加入数据了 我们看view的代码
Login:
<h2>登录-zilchwei</h2>
用户名:<input id="username" type="text" /><br />
密 码:<input id="password" type="password" /><br />
<a href="javascript:checkall()">点我注册</a>
<br />
<div id="regs" style=" display:none">
<% Html.RenderAction("reg", "home"); %>
</div>
<script type="text/javascript">
function checkall() {
document.getElementById("regs").style.display = "";
}
</script>
用户名:<input id="username" type="text" /><br />
密 码:<input id="password" type="password" /><br />
<a href="javascript:checkall()">点我注册</a>
<br />
<div id="regs" style=" display:none">
<% Html.RenderAction("reg", "home"); %>
</div>
<script type="text/javascript">
function checkall() {
document.getElementById("regs").style.display = "";
}
</script>
我们这里有一个javascript用户对<div id="regs">层的控制
<div id="regs">中有RenderAction方法
<% Html.RenderAction("reg", "home"); %>
最后我们再看
reg:
<h2>zilchwei-注册界面</h2>
用户名:<input type="text" /><br />
密码:<input type="password" /><br />
确认密码<input type="password" /><br />
真实姓名:<input type="text" /><br />
邮箱地址<input type="text" /><br />
联系电话<input type="text" /><br />
等等..当然后面还有您需要收集的东西我就不列举了
用户名:<input type="text" /><br />
密码:<input type="password" /><br />
确认密码<input type="password" /><br />
真实姓名:<input type="text" /><br />
邮箱地址<input type="text" /><br />
联系电话<input type="text" /><br />
等等..当然后面还有您需要收集的东西我就不列举了
我们运行看下结果home/login如图
单击 点我注册后结果
当然 我们还会介绍一个功能
我们在homeController中Reg ActionResult中做如下修改
[ChildActionOnly]
public ActionResult reg()
{
return View();
}
public ActionResult reg()
{
return View();
}
下面 我们在浏览器中访问http://localhost/home/reg
这样我们的action是否更安全呢?
今天就到这,愿大家五一快乐
转载于:https://www.cnblogs.com/ZilchWei/articles/1723514.html