简体   繁体   中英

Model properties not displaying with MVC4

I just started learning ASP.NET MVC4 today. After reading tutorials, downloading VS 2012, and creating my first MVC4 app, my model data isn't displaying on the web page.

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. Any idea what I'm missing?

You shouldn't use the HtmlHelper extension method "Display" to display the string. 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

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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