これはゼロからの最初の MVC プロジェクトであり、最初に読み込まれたときに複数の繰り返しレコード データをビューに表示し、編集ボタンをクリックしてデータを保存したときにユーザーが同じページのフィールドを編集できるようにしようとしています。その特定のレコード。いくつかのデータが表示されていますが、間違った方法で行っているように感じます。
これは私の GeneRuleViewModel.cs です
public class GeneRuleViewModel
{
public virtual IEnumerable<GeneRule> GeneRules { get; set; }
public virtual IEnumerable<GeneGroup> GeneGroups { get; set; }
public List<KeyValuePair<int, string>> RuleDataStarDesignation { get; set; }
public List<KeyValuePair<int, IEnumerable<SelectListItem>>> RuleDataPhenotype { get; set; }
public List<KeyValuePair<int, bool>> RuleDataClinicallySignificant { get; set; }
[DisplayName("Star Designation:")]
public string StarDesignation { get; set; }
[DisplayName("Phenotype:")]
public string SelectedPhenotype { get; set; }
[DisplayName("ClinicallySignificant?")]
public bool ClinicallySignificant { get; set; }
}
ビュー内のアイテムをループするときに、どの値が特定の GeneRule_ID に属しているかを知ることができるように、KeyValuePair を使用しました。
これは、リポジトリから KeyValuePairs を設定している GeneRuleController.cs の Index() メソッドです。
public ActionResult Index()
{
var geneRules = generuleRepository.GetGeneRules();
var geneGroups = generuleRepository.GetGeneGroups();
List<KeyValuePair<int, string>> ruleStarDesignation = new List<KeyValuePair<int, string>>();
List<KeyValuePair<int, IEnumerable<SelectListItem>>> rulePhenotype = new List<KeyValuePair<int, IEnumerable<SelectListItem>>>();
List<KeyValuePair<int, bool>> ruleClinicallySignificant = new List<KeyValuePair<int, bool>>();
foreach (var rule in geneRules)
{
rulePhenotype.Add(new KeyValuePair<int, IEnumerable<SelectListItem>>((int)rule.GeneRule_ID, generuleRepository.GetRulePhenotypes(rule)));
ruleStarDesignation.Add(new KeyValuePair<int, string>((int)rule.GeneRule_ID, generuleRepository.GetRuleStarDesignation(rule)));
ruleClinicallySignificant.Add(new KeyValuePair<int, bool>((int)rule.GeneRule_ID, generuleRepository.GetRuleClinicalSignificance(rule)));
}
var generuleViewModel = new GeneRuleViewModel();
generuleViewModel.GeneRules = geneRules;
generuleViewModel.GeneGroups = geneGroups;
generuleViewModel.RuleDataStarDesignation = ruleStarDesignation;
generuleViewModel.RuleDataPhenotype = rulePhenotype;
generuleViewModel.RuleDataClinicallySignificant = ruleClinicallySignificant;
return View(generuleViewModel);
}
これは、各 GeneGroups と GeneRules をループしてデータを表示する Index.cshtml です。
<div id="generulesgrid">
<span class="glyphicon glyphicon-filter"></span> <span class="h4">Rule Filter</span>
<div class="btn-group rulefilter">
<button type="button" class="filter btn btn-default" data-filter="all">Show All</button>
@foreach (var geneGroup in Model.GeneGroups) {
<button type="button" class="filter btn btn-default" data-filter="@Html.DisplayFor(x => geneGroup.GeneGroup_NM)">@Html.DisplayFor(x => geneGroup.GeneGroup_NM)</button>
}
</div>
@foreach (var geneGroup in Model.GeneGroups) {
<div class="mix @Html.DisplayFor(x => geneGroup.GeneGroup_NM)">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<span class="glyphicon glyphicon-list"></span> <span class="h4">Gene Rules for <small>@Html.DisplayFor(x => geneGroup.GeneGroup_NM)</small></span>
</div>
</div>
</div>
<div class="row">
@foreach(var geneRule in Model.GeneRules.Where(x => x.GeneGroup_ID == geneGroup.GeneGroup_ID))
{
<div class="col-md-4">
@using (Html.BeginForm(null, "generule", FormMethod.Post, new { @class = "form-horizontal", @role = "form" }))
{
<div class="panel panel-default">
<div class="panel-heading">
@Html.DisplayFor(x=> geneRule.GeneRule_NM) <span class="glyphicon glyphicon-edit pull-right editRule" data-toggle="tooltip" title="Edit Rule"></span>
</div>
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(x => x.StarDesignation, new { @class = "col-md-4 control-label" })
<div class="col-md-8">
@Html.TextBoxFor(x => x.StarDesignation, new {@Value = Model.RuleDataStarDesignation.Where(x => x.Key == geneRule.GeneRule_ID).FirstOrDefault().Value, @class = "form-control", @placeholder = "Enter Star Designation"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.SelectedPhenotype, new { @class = "col-md-4 control-label" })
<div class="col-md-8">
@Html.DropDownListFor(x=>x.SelectedPhenotype,Model.RuleDataPhenotype.Where(x => x.Key == geneRule.GeneRule_ID).FirstOrDefault().Value,"select phenotype",new {@id = "generule_" + geneRule.GeneRule_ID + "_phenotype", @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.Label("Rule Definition","Rule Definition:",new { @class = "col-md-4 control-label" })
<div class="col-md-8">
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
@Html.CheckBoxFor(x=> x.ClinicallySignificant, Model.RuleDataClinicallySignificant.Where(y => y.Key == geneRule.GeneRule_ID).FirstOrDefault().Value)
</label>
</div>
</div>
</div>
</div>
}
</div>
}
</div>
</div>
}
</div>
私が言ったように、私はこれを間違った方法で行っているように感じますので、どんな助け/アドバイスも大歓迎です.