私は、Pathプロパティのバインドにまったく関与していなかったので、Samsの答えを最初に推奨していました。 Pathプロパティを使用して値を連結すると、レイジーローディングが発生する可能性があることに言及しました。したがって、ドメインモデルを使用してビューに情報を表示していると思います。したがって、ビューモデルを使用してビューに必要な情報のみを表示し(Samsアンサーを使用してパスを取得する)、ビューモデルをツール(つまりAutoMapper)を使用してドメインモデルにマッピングすることをおすすめします。
ただし、ビューで既存のモデルを引き続き使用し、モデル内の他の値を使用できない場合は、カスタムモデルバインダーのフォーム値プロバイダーが提供する値にpathプロパティを設定できます。他のバインドが発生しています(パスプロパティに対して検証を実行しないと仮定します)。
@using (Html.BeginForm())
{
<p>Parent Path: @Html.EditorFor(m => m.ParentPath)</p>
<p>Slug: @Html.EditorFor(m => m.Slug)</p>
<input type="submit" value="submit" />
}
そして、次のビューモデル(または場合によってはドメインモデル)::
パブリッククラスIndexViewModel { パブリック文字列ParentPath {
だからあなたは次のビューを持っていると仮定することができます取得する;セット; } 公開ストリングSlug {get;セット; } パブリック・ストリングPath {get;セット; } }
その後、次のモデルバインダー指定することができます。
public class IndexViewModelBinder : DefaultModelBinder
{
protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//Note: Model binding of the other values will have already occurred when this method is called.
string parentPath = bindingContext.ValueProvider.GetValue("ParentPath").AttemptedValue;
string slug = bindingContext.ValueProvider.GetValue("Slug").AttemptedValue;
if (!string.IsNullOrEmpty(parentPath) && !string.IsNullOrEmpty(slug))
{
IndexViewModel model = (IndexViewModel)bindingContext.Model;
model.Path = bindingContext.ValueProvider.GetValue("ParentPath").AttemptedValue + "/" + bindingContext.ValueProvider.GetValue("Slug").AttemptedValue;
}
}
}
をそして最後に、このモデルバインダーは、ビューモデルに、次の属性を使用して、使用することを指定します。
[ModelBinder(typeof(IndexViewModelBinder))]
'page.Parent.Path'と' page.Slug'がフォームからバインドされており、 'page.Path'をバインド直後にコンテンツの連結に設定したいと言うのは正しいでしょうか?つまり、 'page.Path'の値はフォーム上に存在しません。 – Dangerous
@Dangerous correct、 'page.Path'はフォームにありません。私は 'page.Parent.Id'と' page.Slug'をフォームから取得します。 –
そして、 'page.Parent'と' page.Slug'の後に 'page.Path'を構築したいと思います。 –