简体   繁体   English

模型属性未显示MVC4

[英]Model properties not displaying with MVC4

I just started learning ASP.NET MVC4 today. 我今天刚开始学习ASP.NET MVC4。 After reading tutorials, downloading VS 2012, and creating my first MVC4 app, my model data isn't displaying on the web page. 在阅读教程,下载VS 2012并创建我的第一个MVC4应用程序后,我的模型数据未显示在网页上。

Model 模型

namespace DistributorManagement.Models
{
    public class Vaporizer
    {
        public int Id { get; set; }    
        public string Manufacturer { get; set; }
    }
}

Controller 调节器

public class VaporizerController : Controller
{
    public ActionResult Index()
    {
        Vaporizer vaporizer = new Vaporizer();
        vaporizer.Id = 1;
        vaporizer.Manufacturer = "Acme";

        return View(vaporizer);
    }
}

View 视图

@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)

The Web Page 网页

在此输入图像描述

I tried a few variations of displaying the properties of a Vaporizer, but nothing ever displays on the web page. 我尝试了一些显示Vaporizer属性的变体,但网页上没有显示任何内容。 Any idea what I'm missing? 知道我错过了什么吗?

You shouldn't use the HtmlHelper extension method "Display" to display the string. 您不应使用HtmlHelper扩展方法“Display”来显示字符串。 This method takes "An expression that identifies the object that contains the properties to display" as a string parameter, and not a string to actually display. 此方法将“一个表达式标识包含要显示的属性的对象”作为字符串参数,而不是实际显示的字符串。

cf : http://msdn.microsoft.com/en-us/library/ee310174.aspx cf: http//msdn.microsoft.com/en-us/library/ee310174.aspx

In your case you should just display the string like this : 在您的情况下,您应该只显示如下字符串:

<label>Vaporizer ID</label>
@Model.Id
<br />
<label>Manufacturer</label>
@Model.Manufacturer

And should work fine ! 应该工作正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM