2017-04-26 3 views
1

私はRazorコードに値を表示しようとしています。私はDisplayForとLabelForの両方を試しました。 DisplayForを使用すると、値が表示されますが、CSSは適用されません。私がLabelForを使用する場合、それは逆ですが、私のCSSは適用されますが、テキストは "EmployeeId"としてのみ表示されます。 ViewModelにViewに行く前にEmployeeIdに値が設定されていることを確認しました。Labelは値を表示しません。

これは私が試したものです:すべてのDisplayFor

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

答えて

2

は最初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> 
+1

ちょうどFYI: 'DisplayFor'は* *テンプレート-helperです。つまり、出力は「表示テンプレート」に基づいています。ほとんどのプロパティタイプのデフォルトの表示テンプレートは、HTMLなしで値を出力します。その結果、実際にあなたがそれを渡すことを許可したとしても、htmlAttributesパラメータを適用するものは何もありません。独自のカスタム表示テンプレートを作成してHTMLをインクルードすることはできますが、それはカスタムテンプレートなので、 HTML上の属性を手動で設定します。 'DisplayFor'は値を渡すのに使うことができる' additionalViewData'を受け入れます。 –

+0

ありがとう、両方の答えは正しいようですが、DisplayForが私の必要とするものです。私は実際に2人のヘルパーの違いを理解できるように、すべての余分な情報に感謝します。 – Caverman

0

のようにそれを行うことができます。

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); 
    } 

ビュー:

@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> 
関連する問題