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(); }