asp.net MVC 二 通過ViewData.Model傳值

來源:互聯網
上載者:User

一、先在Models檔案夾下建立一個Model:   Member.cs

  using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;</p><p>namespace MvcApplication2.Models<br />{<br /> public class Member<br /> {<br /> /// <summary><br /> /// 模型的一些屬性,名稱無需與資料庫欄位對應,但為了易讀建意與資料庫欄位相同。<br /> /// </summary><br /> public string name { get; set; }</p><p> public string age { get; set; }</p><p> public string sex { get; set; }<br /> }<br />}

Member可以在預設的Models中的AccountModel.cs中建立,也可以建立一個類檔案,上面的代碼是建立一個檔案,

二、接下來我們再建立一個Action:MemberIndex()

如果建立的類檔案在原默的檔案中則MemberIndex也要建立在相應的Controller中,如果是單獨建立的類檔案則,在相應的Controller中建讓Action如下:

public ActionResult MemberIndex()<br /> {<br /> MvcApplication2.Models.Member m = new MvcApplication2.Models.Member();<br /> m.name = "張三";<br /> m.sex = "男";<br /> m.age = "24";<br /> return View(m);<br /> }

 三、接下來再為Action建立View分頁檔:

      1、名稱:MemberIndex   與Action相同

 

添加後產生頁面:

 <fieldset>
        <legend>Fields</legend>
       
        <div class="display-label">name:<%= Html.Encode(Model.name) %></div>
        <div class="display-field"></div>
       
        <div class="display-label">age:<%= Html.Encode(Model.age) %></div>
        <div class="display-field"></div>
       
        <div class="display-label">sex:<%= Html.Encode(Model.sex) %></div>
        <div class="display-field"></div>
       
    </fieldset>
    <p>
        <%= Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) %> |
        <%= Html.ActionLink("Back to List", "Index") %>
    </p>

運行,http://localhost:4116/admin/memberindex

下一篇:asp.net MVC 三 ActionResult返回其他值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.