MVC 2.0中页面传值

Coordinator
Aug 24, 2011 at 8:22 AM

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

1.TempData和ViewData的应用

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

Action代码:

view plaincopy to clipboardprint?
public ActionResult Index()  
{  
    ViewData["Message"] = "Welcome to ASP.NET MVC!";  
    return View();  

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

页面代码:

view plaincopy to clipboardprint?
<h2><%= Html.Encode(ViewData["Message"]) %></h2> 
 <h2><%= Html.Encode(ViewData["Message"]) %></h2>
TempData使用方式与View使用方式一致.

2. ViewData与TextBox实现自动绑定

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

view plaincopy to clipboardprint?
Name:<%= Html.TextBox("name") %> 
Name:<%= Html.TextBox("name") %>

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

view plaincopy to clipboardprint?
Name:<%= Html.TextBox("name", ViewData["Nm"]) %> 
Name:<%= Html.TextBox("name", ViewData["Nm"]) %>

3.View向Controller传值

1). 利用Action参数

view plaincopy to clipboardprint?
<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();  

        <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 ASP.NET MVC!";
            ViewData["name"] = name;
            ViewData["sex"] = sex;
            return View();
        }

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

view plaincopy to clipboardprint?
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()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";
            ViewData["name"] = Request.Form["name"];
            ViewData["sex"] = Request.Form["sex"];
            return View();
        }

3). 利用FormCollection获取页面值

view plaincopy to clipboardprint?
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 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);
        }

4.传递强类型

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

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

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

view plaincopy to clipboardprint?
<p>  
<%User u = (User)ViewData.Model;%>  
            UserName:   
            <%= Html.Encode(u.UserName) %>  
        </p>  
        <p>  
            Password:   
            <%= Html.Encode(u.Password) %>  
        </p 
<p>
<%User u = (User)ViewData.Model;%>
            UserName:
            <%= Html.Encode(u.UserName) %>
        </p>
        <p>
            Password:
            <%= Html.Encode(u.Password) %>
        </p