简体   繁体   English

LabelFor不显示价值?

[英]LabelFor not displaying value?

I'm trying to just display a value in my Razor code. 我试图在我的Razor代码中显示一个值。 I've tried both DisplayFor and LabelFor. 我已经尝试过DisplayFor和LabelFor。 If I use DisplayFor then my value shows up but my CSS doesn't get applied. 如果我使用DisplayFor,则会显示我的值,但不会应用CSS。 If I use LabelFor then it's the opposite, my CSS is applied but the text only displays as "EmployeeId". 如果我使用LabelFor,则相反,将应用我的CSS,但文本仅显示为“ EmployeeId”。 I've confirmed that my ViewModel has a value populated for the EmployeeId prior to going to View. 我已经确认,在进入View之前,我的ViewModel具有为EmployeeId填充的值。

This is what I've tried: 这是我尝试过的:

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: @Html.LabelFor(m => m.EmployeeId, new { @class = "control-label label-value" })
    </div>
</div>

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: @Html.DisplayFor(m => m.EmployeeId, new { @class = "control-label label-value" })
    </div>
</div>

first of all DisplayFor does not have an overload of htmlAttributes so new { @class = "control-label label-value" } wont work and secondly LabelFor does not display value of the property it will just display the name of the property so you can do it like this 首先DisplayFor没有htmlAttributes的重载,所以new { @class = "control-label label-value" }不起作用,其次LabelFor不显示属性的值,它将仅显示属性的名称,因此您可以像这样做

<div class="row voffset2">
    <div class="col-md-12">
        Employee ID: <span class"ontrol-label label-value">@Html.DisplayFor(m => m.EmployeeId)</span>
    </div>
</div>

This will work: 这将起作用:

public class LabelViewModel
{
    [Display(Name = "Employee\nId")]
    public int EmployeeId { get; set; }

    //display for is for iterating over a collection
    public IList<string> Employees { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index54()
    {
        //display for is for iterating over a collection
        LabelViewModel labelViewModel = new LabelViewModel();
        labelViewModel.Employees = new List<string>();
        labelViewModel.Employees.Add("emp1");
        labelViewModel.Employees.Add("emp2");
        return View(labelViewModel);
    }

View: 视图:

@model Testy20161006.Controllers.LabelViewModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index54</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style type="text/css">
        .cssworks {
            color: red
        }
    </style>
</head>
<body>
    <div class="row voffset2">
        <div class="col-md-12">
            Employee ID: @Html.LabelFor(m => m.EmployeeId, new { @class = "cssworks control-label label-value" })
        </div>
    </div>

    <div class="row voffset2">
        <div class="col-md-12">
            @*display for is for iterating over a collection*@
            @*changed the next line, since you can't put a class in displayfor*@
            Employee ID: <span class="cssworks">@Html.DisplayFor(m => m.Employees)</span>
        </div>
    </div>
</body>
</html>

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

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