0

考えてくれてありがとう。

いくつかのカスタムvalidationAttributesを処理しているときに、単純なはずの問題に遭遇しましたが、困惑しました。

許可されたユーザーは、作業しているサイトへのキーを含むUserProfileを持ちます。このサイトは、データベース内に設定されたレコードです。このサイトレコードセットの1つのフィールドは、完全に別のテーブルのフィールドの有効な形式を示す正規表現です。この他のテーブルに入力されたデータは、すべての登録ユーザーに共通ですが、特定のフィールドは、所属機関で使用されているID形式に関連しています。

正規表現バリデーターをプロパティに動的に追加できるクリーンな方法はありますか?

いつもありがとうございます。

4

1 に答える 1

0

これは私が思いついたものですが、より良い解決策があるかどうかを知りたがっています:

命名規則は、オートマッパーがモデルをフラット化できるようにすることです(各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>
于 2012-08-13T06:44:17.947 に答える