300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > ASP.NET MVC学习笔记-Controller与View传值

ASP.NET MVC学习笔记-Controller与View传值

时间:2020-12-18 10:27:44

相关推荐

ASP.NET MVC学习笔记-Controller与View传值

在2.0的网页开发模式下,我们一般通过直将访问页面控件, 将值写入到页面, 但在 MVC模式下,已不能在Controller中再去访问页面控件了,要如何才能给View页面传值呢?

在Controller中有两个字典(ViewData和TempData)来实现View之间的值传递, 在ControllerBase类声明中我们就可以看到:namespace System.Web.Mvc{// 摘要://Represents the base class for all MVC controllers.public abstract class ControllerBase : MarshalByRefObject, IController{protected ControllerBase();// 摘要://Gets or sets the controller context.public ControllerContext ControllerContext { get; set; }//// 摘要://Gets or sets the temporary data.public TempDataDictionary TempData { get; set; }//// 摘要://Gets or sets a value indicating whether the request is valid.public bool ValidateRequest { get; set; }//// 摘要://Gets or sets the value provider.public IDictionary<string, ValueProviderResult> ValueProvider { get; set; }//// 摘要://Gets or sets the view data.public ViewDataDictionary ViewData { get; set; }// 摘要://Executes the specified request context.//// 參數:// requestContext://The request context.protected virtual void Execute(RequestContext requestContext);//// 摘要://Executes the core.protected abstract void ExecuteCore();//// 摘要://Initializes the specified request context.//// 參數:// requestContext://The request context.protected virtual void Initialize(RequestContext requestContext);}}

而在ViewResultBase中我们同样可以看到它也含有这两个字典存在:

// 摘要://Base class used to supply the model to the view and then render the view//to the response.public abstract class ViewResultBase : ActionResult{protected ViewResultBase();// 摘要://Gets or sets the System.Web.Mvc.TempDataDictionary for this result.public TempDataDictionary TempData { get; set; }//// 摘要://Gets or sets the System.Web.Mvc.IView that is rendered to the response.public IView View { get; set; }//// 摘要://Gets or sets the view data System.Web.Mvc.ViewDataDictionary for this result.public ViewDataDictionary ViewData { get; set; }//// 摘要://Gets or sets the view engines (System.Web.Mvc.ViewEngineCollection) associated//with this result.public ViewEngineCollection ViewEngineCollection { get; set; }//// 摘要://Gets or sets the name of the view to be rendered.public string ViewName { get; set; }// 摘要://When called by the action invoker, renders the view to the response.//// 參數:// context://The context within which the result is executed.public override void ExecuteResult(ControllerContext context);//// 摘要://When overridden, returns the System.Web.Mvc.ViewEngineResult used to render//the view.//// 參數:// context://The context.//// 傳回://The view engine.protected abstract ViewEngineResult FindView(ControllerContext context);}

由此可以看出,Controller通过ViewData,TempData传通到ViewResult中, 然后再由ViewResult传递到ViewPage中来实现值传递的。

1.TempData和ViewData的应用

ViewData只对当前Action有效,而TempData有点类似于Session, 可在所有View访问, 一般用于记录错误信息.

Action代码:

public ActionResult Index(){ViewData["Message"] = "Welcome to MVC!";return View();}

页面代码:

<h2><%= Html.Encode(ViewData["Message"]) %></h2>

TempData使用方式与View使用方式一致.

2. ViewData与TextBox实现自动绑定

利用HtmlHelper创建TextBox时,使用名称与ViewData中的Key一致, 就会自动实现值绑定,如:

Name:<%= Html.TextBox("name") %>

名称不相同的情况下,也可以利用TextBox的重载传值:

Name:<%= Html.TextBox("name", ViewData["Nm"]) %>

3.View向Controller传值

1). 利用Action参数

<form name="form1" action="/Home/Index" method="post">Name:<input type="text" name="name" /><br />Sex: <input type="text" name="sex" /><input type="submit" value="submit" /></form><%if (ViewData["name"] != null){Response.Write("your name is:" + ViewData["name"] + ", your sex is:" + ViewData["sex"]);} %>Action代码:public ActionResult Index(string name, string sex){ViewData["Message"] = "Welcome to MVC!";ViewData["name"] = name;ViewData["sex"] = sex;return View();}

2).利用Request.From或Request.QueryString

public ActionResult Index(){ViewData["Message"] = "Welcome to MVC!";ViewData["name"] = Request.Form["name"];ViewData["sex"] = Request.Form["sex"];return View();}

3). 利用FormCollection获取页面值

public ActionResult Index(FormCollection form){ViewData["Message"] = "Welcome to MVC!";User u=new User();u.Name = form["Name"];u.Password = form["Password"];return View(u);}

4.传递强类型

1).添加一个传递强类型Model的Action

public ActionResult ModelDemo(){User u= new User() { UserName="li", Password="abcde" };return View(u);}

对应的View也需要继随于ViewPage<User>, 对应代码如下:

<p><%User u = (User)ViewData.Model;%>UserName: <%= Html.Encode(u.UserName) %></p><p>Password: <%= Html.Encode(u.Password) %></p

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。