まず、MVCがModel、View、Controllerの略であるASP.Net MVCを使用しています。
表示されているコードから明らかに、ビュー(Index.cshtml)とコントローラ(HomeController.cs)があります。あなたはモデルがありません。
ビューに表示する値を含むモデルを作成する必要があります。あなたのビューで正しい場所で
@Model.Value
:私はあなたがそうのような何かを書く、あなたのモデルから値を描画するために、Razorを使用していることを前提としています。
あなたのモデルと状態を色にマップするための計算されたプロパティを含む新しいクラスを作成することをお勧めします。
namespace Your.Namespace
{
// use some nice name for your model
public class BoxStatusModel
{
public int BoxID { get; set; }
public string CurrentStatus { get; set; }
public string Color
{
get
{
switch(CurrentStatus)
{
case "Ok":
return "Green";
case "Warning":
return "Yellow";
case "Error":
return "Red";
default:
return "";
}
}
}
}
}
そして、あなたのコントローラであなたは次のように移入されます:次に、あなたはあなたのビューで使用しているモデル・タイプを定義する必要があり、あなたがプロパティにアクセスすることができるようになります
public class HomeController : Controller
{
public ActionResult Index()
{
MySQL msql = new MySQL();
var results = msql.SelectList("Select columns from table");
// create your collection of the correct size since the beginning
var model = new List<BoxStatusModel>(results.Count);
for (int i = 0; i < results.Count; i++)
{
model.Add(new BoxStatusModel() {
// I am not familiar with the MySQL but should be something like this
BoxID = results[i].GetValue("BoxID");
CurrentStatus = results[i].GetValue("CurrentStatus");
});
}
return View(model);
}
}
ような何かそれらを使用してステータスをカラーとしてレンダリングします。あなたはまた、各色のCSSクラスを作成する必要があります。この場合
<!-- Your model is a list of BoxStatusModels -->
@model List<Your.Namespace.BoxStatusModel>
[...]
<table>
[...] // table headers and stuff
<tr>
<td class="dclass">
@foreach(var boxStatus in Model)
{
<div id="@boxStatus.BoxID" class="top @boxStatus.Color">
}
</td>
</tr>
[...] // table footer?
</table>
:
.Red {
background-color: Red;
}
.Yellow {
background-color: Yellow;
}
.Green {
background-color: Green;
}
他の可能性は直接CurrentStatusプロパティを使用し、クラス名としてそれをレンダリングすることです。あなたはまた、各ステータスのためのCSSクラスを作成する必要があります。この場合
@model List<Your.Namespace.BoxStatusModel>
[...]
<table>
[...] // table headers and stuff
<tr>
<td class="dclass">
@foreach(var boxStatus in Model)
{
<div id="@boxStatus.BoxID" class="top @boxStatus.CurrentStatus">
}
</td>
</tr>
[...] // table footer?
</table>
ような何か:
.Error {
background-color: Red;
}
.Warning {
background-color: Yellow;
}
.Ok {
background-color: Green;
}
別(メシエ)オプションは、カラープロパティを使用してのスタイル属性でそれをレンダリングしていますdiv。同様:
@model List<Your.Namespace.BoxStatusModel>
[...]
<table>
[...] // table headers and stuff
<tr>
<td class="dclass">
@foreach(var boxStatus in Model)
{
<div id="@boxStatus.BoxID" class="top" style="background-color:@boxStatus.color">
}
</td>
</tr>
[...] // table footer?
</table>
アイデアは、あなたのビューで好きな形であなたのコントローラは、データベースからモデルを移入して、そのプロパティを(テキストとして)レンダリングすることができるということです。
これはレンダリング時または自分の側にあるときに発生しますか? (サーバー側またはクライアント側) – ajputnam
私はそれがサーバー側で発生します。 – NinjaDev
OK、私はいくつかの質問をしています:1)現在、あなたの 'Index.cshtml'ビューにこの情報をどのように出力していますか? - いくつかのコードを投稿できますか? 2)MVC *や* MVVM、WPF、その他では、ViewModelsを使い慣れた/使用していますか? 3)あなたのテーブルにはどのようなコラムがありますか?現時点では、あなたが「ステータス」とそれが何を所有しているのかを見せたいと思っているのかどうかは分かりません。ありがとう:) –