![](/img/trans.png)
[英]How to access different model properties in same view in mvc4?Is it possible?
[英]Model properties not displaying with MVC4
我今天剛開始學習ASP.NET MVC4。 在閱讀教程,下載VS 2012並創建我的第一個MVC4應用程序后,我的模型數據未顯示在網頁上。
模型
namespace DistributorManagement.Models
{
public class Vaporizer
{
public int Id { get; set; }
public string Manufacturer { get; set; }
}
}
調節器
public class VaporizerController : Controller
{
public ActionResult Index()
{
Vaporizer vaporizer = new Vaporizer();
vaporizer.Id = 1;
vaporizer.Manufacturer = "Acme";
return View(vaporizer);
}
}
視圖
@model DistributorManagement.Models.Vaporizer
@{
ViewBag.Title = "Vaporizer";
}
<h2>Snuh</h2>
<label>Vaporizer ID</label>
@Html.Display(Model.Id.ToString())
<br />
<label>Manufacturer</label>
@Html.Display(Model.Manufacturer)
網頁
我嘗試了一些顯示Vaporizer屬性的變體,但網頁上沒有顯示任何內容。 知道我錯過了什么嗎?
您不應使用HtmlHelper擴展方法“Display”來顯示字符串。 此方法將“一個表達式標識包含要顯示的屬性的對象”作為字符串參數,而不是實際顯示的字符串。
cf: http : //msdn.microsoft.com/en-us/library/ee310174.aspx
在您的情況下,您應該只顯示如下字符串:
<label>Vaporizer ID</label>
@Model.Id
<br />
<label>Manufacturer</label>
@Model.Manufacturer
應該工作正常!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.