2013-07-14 60 views
0

我只是想在ASP.net(ASPX)中做一些非常簡單的事情,而且它似乎沒有工作。如何將我的用戶對象數據傳遞給ASPX頁面?

我有以下的類/模型,視圖和控制器:

Users.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ASPWebsite.Models 
{ 
    public class Users 
    { 
     private string firstName; 
     private string lastName; 

     public Users(string firstName, string lastName) 
     { 
      this.firstName = firstName; 
      this.lastName = lastName; 
     } 

     public string GetFirstName() 
     { 
      return this.firstName; 
     } 

     public string GetLastName() 
     { 
      return this.lastName; 
     } 
    } 
} 

的Index.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ASPWebsite.Models.Users>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Index 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>Index</h2> 

<h2><%Model.GetFirstName();%></h2> 

</asp:Content> 

HomeController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

using ASPWebsite.Models; 

namespace ASPWebsite.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      var user = new Users("foo", "bar"); 

      return View(user); 
     } 

     public ActionResult About() 
     { 
      return View(); 
     } 
    } 
} 

正如您所看到的,我創建了一個名爲foo bar的用戶並將其傳遞給視圖,但是當我運行它時,它不顯示名稱。它有什麼問題?

回答

3

嘗試<h2><%: Model.GetFirstName() %></h2>

+0

現在我得到編譯器錯誤信息:CS1026:)預計 – Danny

+0

NVM,忘了刪除;最後謝謝 – Danny

相關問題