一、先在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返回其他值