2013-07-14 2 views
0

Я просто пытаюсь сделать что-то очень простое здесь в ASP.net (ASPX), и он, похоже, не работает.Как передать данные объекта User на страницу ASPX?

У меня есть следующий класс/Model, View и Controller:

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

Смежные вопросы