これは私が思いついたものですが、より良い解決策があるかどうかを知りたがっています:
命名規則は、オートマッパーがモデルをフラット化できるようにすることです(各StudyCentreはRecordSystemと多対1の関係にあります(一部のシステムは患者インデックスシステムを共有します)
Mapper.CreateMap<StudyCentre, ParticipantRegistration.StudyCentreViewData>();
個々のTrialParticipantのViewModel内のネストされたクラスとして
public StudyCentreViewData ViewData { get; set; }
public class StudyCentreViewData
{
public string Abbreviation { get; set; }
public string RecordSystemName { get; set; }
public string RecordSystemHospitalNoRegEx { get; set; }
public string RecordSystemNotationDescription { get; set; }
public IDictionary<string, object> HospitalNoRegEx()
{
return DynamicClientValidation.RegEx(errorMessage:String.Format("{0} must consist of {1}",
RecordSystemName,
RecordSystemNotationDescription),
regExPattern: RecordSystemHospitalNoRegEx);
}
}
その他のプロパティ(StudyCentre.Abbreviationはラベル用など)関数RegExは単純に次のとおりです。
public static class DynamicClientValidation
{
public static IDictionary<string, object> RegEx(string errorMessage, string regExPattern)
{
var returnVal = new Dictionary<string, object>(3);
returnVal.Add("data-val-regex", errorMessage);
returnVal.Add("data-val-regex-pattern", regExPattern);
returnVal.Add("data-val", "true");
return returnVal;
}
}
コントローラは、次のようにビューモデルを設定します。
model.ViewData = Mapper.Map<StudyCentre, ParticipantRegistration.StudyCentreViewData>(_studyCentre.GetCentreByUser(_currentUserName));
ビュー内(LabelDetailsforはカスタムヘルパーです):
<div class="editor-label">
@Html.LabelDetailsFor(model => model.HospitalID,Model.ViewData.Abbreviation + " ID", Model.ViewData.RecordSystemName)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.HospitalID, Model.ViewData.HospitalNoRegEx())
@Html.ValidationMessageFor(model => model.HospitalID)
</div>