一,viewdata,viewbag與tempdata

 

asp.net mvc架構中,通過繼承在controller中的viewdata,viewbag和tempdata和view頁面進行資料的存取,並且適合於少量的資料傳遞。

 

1.1  viewbag

 

viewbag可以產生動態屬性,我們新建項目中看到viewbag的使用方法:

 

controller中賦值:viewbag.title=”首頁”  view中獲取值 @viewbag.title

 

1.2  viewdata

 

controller中賦值:viewdata[“message”]=”this is viewdata value”;

 

view頁面中取值:@viewdata[“message”]

 

1.3  tempdata

 

和viewbag,viewdata不同的是,tempdata預設把資料存放於session,

 

其生命週期存在於以整個request的範圍,可以在controller和controller之間做資料的傳遞

 

 

复制代码

  public actionresult index()

        {

            //viewdata

            viewdata[“viewdatavalue”] = “this is viewdata value”;

            //tempdata

            tempdata[“tempdatavalue”] = “this is tempdata value”;

            //viewbag

            viewbag.message = “修改此範本即可開始著手進行您的 asp.net mvc 應用程式。”;

 

            return view();

        }

复制代码

 

复制代码

 <hgroup class=”title”>

                <h1>@viewbag.title.</h1>

                <h2>@viewbag.message</h2>

                <h3>@viewdata[“viewdatavalue”]</h3>

                <h3>@tempdata[“tempdatavalue”]</h3>

                

 </hgroup>

复制代码

二,  模型連接(model binding)

 

asp.net mvc中會使用模型連接(model binding)使controller獲取view中的資料。

 

2.1簡單的模型連接

 

如下示例,view頁面有個id為content的文本框,對應的action中有同名的參數content,這樣當action被執行的時候程序會通過defaultmodelbinder類別把view頁面傳遞過來的資料傳入action中的同名參數。

 

view:

 

@using(html.beginform()){

    <p>@html.label(“請輸入content內容: “)<input type=”text”/ name=”content”></p>

    <p>@html.label(“您輸入的內容為:”)@viewdata[“content”]</p>

    <input  type=”submit” value=”提交”/>

}

action:

 

        public actionresult testaction(string content)

        {

            viewdata[“content”] = content;

            return view();

        }

   

 

我們下斷點看一下,點擊提交以後,會通過簡單的數據連接模型把content文本框中的值傳遞到testaction的參數中,然後通過viewdata[“content”]把值取出。

 

 

 

2.2 formcollection

 

asp.net mvc除了簡單的模型連接取得view頁面資料外,還可以通過formcollection來取得整個客戶端頁面的資料。

 

在action中加入formcollection參數以後即可取得表單資料。

 

view:

@{

    viewbag.title = “testaction”;

}

 

<h2>testaction</h2>

 

@using(html.beginform()){

    <p>@html.label(“請輸入content內容: “)<input type=”text”/ name=”content”></p>

    <p>@html.label(“您輸入的內容為:”)@viewdata[“content”]</p>

    <input  type=”submit” value=”提交”/>

}

复制代码

action:

 

 

  //formcollection

        public actionresult testaction(formcollection form)

        {

            viewdata[“content”] = form[“content”];

            return view();

        }

2.3複雜模型連接

 

1,我們添加testformmodel類,類中定義三個屬性

 

 

复制代码

using system;

using system.collections.generic;

using system.linq;

using system.web;

using system.componentmodel.dataannotations;

 

namespace mapplication3.models

{

    public class testformmodel

    {

       

        public string content { get; set; }

       

        public string userid { get; set; }

 

        public int age { get; set; }

    }

}

复制代码

2,action參數更改為testformmodel類別,來接收view頁面傳遞過來的form表單,

 

     只要form表單裏面的欄位名稱和model類別中的屬性一一對應,即可完成接收動作

 

view:

 

 

复制代码

@{

    viewbag.title = “testform”;

}

 

<h2>testform</h2>

@using (html.beginform())

{

    <p>

        @html.label(“请输入content内容:”)

        <input name=”content” type=”text” />

    </p>

    <p>@html.label(“请输入userid:”)<input name=”userid” type=”text” /></p>

   

    <input type=”submit” value=”提交” />

    <p>

        您提交的内容为:content= @viewdata[“content”]

        <br />

        userid= @viewdata[“userid”]

    </p>

}

复制代码

action,記得添加引用model

 

 

复制代码

    //複雜模型连接

        public actionresult testform(testformmodel form)

        {

            viewdata[“content”] = form.content;

            viewdata[“userid”] = form.userid;

 

            return view();

        }

复制代码