这是我从头开始的第一个 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; }
}
我使用了 KeyValuePair,以便在遍历视图中的项目时,我可以知道哪个值属于特定的 GeneRule_ID
这是我在 GeneRuleController.cs 中的 Index() 方法,我从存储库中填充 KeyValuePairs
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);
}
这是我的 Index.cshtml,我在其中循环遍历每个 GeneGroups 和 GeneRules 以显示数据
<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>
正如我所说,我觉得我好像走错了路,所以任何帮助/建议将不胜感激。