在asp.net2.0的網頁開發模式下,我們一般通過直將訪問頁面控件, 將值寫入到頁面, 但在Asp.net MVC模式下,已不能在Controller中再去訪問頁面控件了,要如何才能給View頁面傳值呢?
- 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);
- }
- }
- // 摘要:
- // 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);
- }
- public ActionResult Index()
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- return View();
- }
- <h2><%= Html.Encode(ViewData["Message"]) %></h2>
TempData使用方式與View使用方式一致.
- Name:<%= Html.TextBox("name") %>
- Name:<%= Html.TextBox("name", ViewData["Nm"]) %>
- <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"]);
- }
- %>
- :
- public ActionResult Index(string name, string sex)
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- ViewData["name"] = name;
- ViewData["sex"] = sex;
- return View();
- }
- public ActionResult Index()
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- ViewData["name"] = Request.Form["name"];
- ViewData["sex"] = Request.Form["sex"];
- return View();
- }
- public ActionResult Index(FormCollection form)
- {
- ViewData["Message"] = "Welcome to ASP.NET MVC!";
- User u=new User();
- u.Name = form["Name"];
- u.Password = form["Password"];
- return View(u);
- }
- public ActionResult ModelDemo()
- {
- User u= new User() { UserName="li", Password="abcde" };
- return View(u);
- }
- <p>
- <%User u = (User)ViewData.Model;%>
- UserName:
- <%= Html.Encode(u.UserName) %>
- </p>
- <p>
- Password:
- <%= Html.Encode(u.Password) %>
- </p