2013-01-11 28 views
0

我創建了一個簡單的類,它包含三個屬性的ViewPage MVC的犯規顯示

ID 
Name 
Amount 

我已經通過了模型的命名空間到控制器類的類屬性,

public class _1Controller : Controller 
    { 
     // 
     // GET: /1/ 
     Customer objCustomer = new Customer(); 
     public ActionResult Index() 
     { 
      ViewData["CurrentTime"] = DateTime.Now.ToString(); 
      return View(); 
     } 

     public ViewResult DisplayCustomer() 
     { 
      Customer objCustomer = new Customer(); 
      objCustomer.ID = 12; 
      objCustomer.Name = "1001"; 
      objCustomer.Amount = 90.34; 

      return View("DisplayCustomer", objCustomer); 
     } 

    } 

當我有控制器類綁定到一個視圖,如

@model MvcApplication4.Models.Customer 

@{ 
    Layout = null; 
} 

<html> 
<head> 
    <title>DisplayCustomer</title> 
</head> 
<body> 
    <div> 
     The customer id is <%= Model.Id %> <br /> 
     the customer Name is <%= M 
    </div> 
</body> 
</html> 

當我試圖調用類屬性時,它不顯示。 我已經點擊強類型checkbox綁定視圖到控制器。感謝您的任何幫助。

+0

我是新來的MVC,解釋將是巨大的。 –

回答

1

MVC3有一些剃刀Html幫手方法來完成這個。

而不是

<%= Model.Id %> 

試試這個

@Html.DisplayFor(model => model.ID) 

Here is a list of these helper on MSDN

+0

還有一件事要補充,如果你還沒有使用[asp.net/mvc](http://www.asp.net/mvc)網站上的教程,那麼給他們一個嘗試,他們是一個偉大的初始點。 – Stokedout