controller的几个实例

1.如何向页面中传递参数?

public ActionResult Index(string id, Nullable<int> other) { this.ViewData["Other"] = other; return View(); }

上面中的id变量是在index页面初始化的时候,通过mvc framework来实现传递的(http://...?id=1)。

2.如何制定form的action函数?

<form action="ShowPostData" method="post"> I D: <input name="UserID" id="UserID" type=text /><br /> Name : <input name="Name" id="Name" type=text /><br /> Password : <input name="Password" id="Password" type="password" /> <input type="submit" type="submit"> </form>

// 这个是在页面初始化的时候调用,通过整个的framework // 实现将整个的数据传递到页面ShowPostData.aspx public ActionResult ShowPostData() { UserIdentity identity = new UserIdentity(); this.UpdateModel<UserIdentity>(identity); this.ViewData["Identity"] = identity; return View(); }

public class UserIdentity { public string UserID { get; set; } public string Name { get; set; } public string Password { get; set; } }

上面在form中制定了数据post的action函数ShowPostData,然后是整个的mvc框架将数据通过UpdateModel传递给entity UserIdentity。(注意的是在UserIdentity类中的id使用的是string类型,测试时使用int类型的话,会产生异常,暂时还没有什么好的办法。)。这样在ShowPostData.aspx中就能够使用这些数据。

<div> ID:<%= ((FirstMVC.Models.UserIdentity)this.ViewData["Identity"]).UserID %><br/> Name:<%= ((FirstMVC.Models.UserIdentity)this.ViewData["Identity"]).Name%><br /> Password:<%= ((FirstMVC.Models.UserIdentity)this.ViewData["Identity"]).Password%> </div>

3.controller类中的错误处理实现?

简单的办法是直接覆盖HandleUnknownAction方法。

protected override void HandleUnknownAction(string actionName) { this.Response.Redirect("~/User/LoginPage"); }

4.如何实现页面的跳转?

public ActionResult ToMicrosoft() { this.Redirect("http://www.microsoft.com"); return View(); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值